How to know where to direct the php form and where to place in html?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gilda Horgan

    #1

    How to know where to direct the php form and where to place in html?

    I am new to PHP and have been reading tutorials all day. I'm trying to write code so my "contact us" form generates an email and sends when clicking on submit. The form is built - I got it from a template and it has been customized, but the PHP is not there yet. Now, I'm trying to marry the php script I have with the html I have to get it to work.

    The problem I'm having is knowing exactly where to place the php script, what to put after action=" " to make it work, and if I put emailto:name@ad dress.com as the href.

    I'm using the script from this site:


    And trying to marry it with my code here:

    to generate an email to info(at)getcrea tivesense.com

    Where I may be making the mistake is having a separate .php doc (thinking it works like java)

    I've read so much info about it today that now I'm completely confused.

    Thank you. Here is the code:

    Script for PHP:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Sendemail Script</title>
    </head>
    <body>
    
    <!-- Reminder: Add the link for the 'next page' (at the bottom) --> 
    <!-- Reminder: Change 'YourEmail' to Your real email --> 
    
    <?php
    
    $ip = $_POST['ip']; 
    $httpref = $_POST['httpref']; 
    $httpagent = $_POST['httpagent']; 
    $visitor = $_POST['visitor']; 
    $visitormail = $_POST['visitormail']; 
    $notes = $_POST['notes'];
    $attn = $_POST['attn'];
    
    
    if (eregi('http:', $notes)) {
    die ("Do NOT try that! ! ");
    }
    if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) 
    {
    echo "<h2>Use Back - Enter valid e-mail</h2>\n"; 
    $badinput = "<h2>Feedback was NOT submitted</h2>\n";
    echo $badinput;
    die ("Go back! ! ");
    }
    
    if(empty($visitor) || empty($visitormail) || empty($notes )) {
    echo "<h2>Use Back - fill in all fields</h2>\n";
    die ("Use back! ! "); 
    }
    
    $todayis = date("l, F j, Y, g:i a") ;
    
    $attn = $attn ; 
    $subject = $attn; 
    
    $notes = stripcslashes($notes); 
    
    $message = " $todayis [EST] \n
    Attention: $attn \n
    Message: $notes \n 
    From: $visitor ($visitormail)\n
    Additional Info : IP = $ip \n
    Browser Info: $httpagent \n
    Referral : $httpref \n
    ";
    
    $from = "From: $visitormail\r\n";
    
    
    mail("YourEmail", $subject, $message, $from);
    
    ?>
    
    <p align="center">
    Date: <?php echo $todayis ?> 
    <br />
    Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> ) 
    <br />
    
    Attention: <?php echo $attn ?>
    <br /> 
    Message:<br /> 
    <?php $notesout = str_replace("\r", "<br/>", $notes); 
    echo $notesout; ?> 
    <br />
    <?php echo $ip ?> 
    
    <br /><br />
    <a href="contact.php"> Next Page </a> 
    </p> 
    
    </body>
    </html>
    And following, the code from my page:
    Code:
    <form id="contacts-form" method=post action="contact.php">
                         <fieldset>
                            <div class="col-1">
     						   <label>Enter Your Name:<br /><input type="text" name="name" value=""/></label>
                               <label>Enter Your E-mail:<br /><input type="text" name="email" value=""/></label>
                               <label>Enter Your Company:<br /><input type="text" name="company" value=""/></label>
                            </div>
                            <div class="col-2">
                               Enter Your Message:<br /><textarea name="message" cols="" rows=""></textarea>
                               <INPUT type="submit" value="Send Mail">&nbsp;&nbsp;<INPUT TYPE="RESET" value="Clear It">
                            </div>
                        </fieldset>
                      </form>
                   </div>
    Do I put the php script in the header, like java? Or, in it's own .php file. I'm unclear on that.

    Here's the html from the site where I got the script. Since my form has <label> and is formatted a little differently, I am not sure how to incorporate the php prompts:

    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Email Form </title>
    </head>
    <body>
    
    <form method="post" action="sendeail.php">
    
    <!-- DO NOT change ANY of the php sections -->
    <?php
    $ipi = getenv("REMOTE_ADDR");
    $httprefi = getenv ("HTTP_REFERER");
    $httpagenti = getenv ("HTTP_USER_AGENT");
    ?>
    
    <input type="hidden" name="ip" value="<?php echo $ipi ?>" />
    <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
    <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
    
    
    Your Name: <br />
    <input type="text" name="visitor" size="35" />
    <br />
    Your Email:<br />
    <input type="text" name="visitormail" size="35" />
    <br /> <br />
    <br />
    Attention:<br />
    <select name="attn" size="1">
    <option value=" Sales n Billing ">Sales n Billing </option> 
    <option value=" General Support ">General Support </option> 
    <option value=" Technical Support ">Technical Support </option> 
    <option value=" Webmaster ">Webmaster </option> 
    </select>
    <br /><br />
    Mail Message:
    <br />
    <textarea name="notes" rows="4" cols="40"></textarea>
    <br />
    <input type="submit" value="Send Mail" />
    <br />
    </form>
    
    </body>
    </html>
    Last edited by Niheel; Nov 3 '10, 03:51 PM.
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Hey there Gilda, welcome to bytes.

    PHP is flexible and free (unlike java and c) that it confuses beginners.

    The html <form> tag's action tells where to send the information to. This can be the same file (or blank) or another place. It doesn't haven have to be PHP. The information is sent using the method set in the form. The two popular ones are "get" and "post". These are just methods of how they're stored in the page header and sent across the internet. GET sends it as part of the URL and POST uses embedded header variables that you can't plainly see. POST is preferred for things like long text fields such as an email message.

    To get to your question, You can create a PHP document in this server/site or any other site on the web to process the information (in your case, email it) that you receive from the form.

    Once the form is submitted, it is like clicking a link. You can land back on the same page and display a message (like "you're email has been sent" or "Error: message cannot be blank")

    In your email you have contact.php as the destination (action) of your form, but it should be sendeail.php if that's the script you want to process your form and send an email. It's NOT included in the header like Java.

    Hope that clarified any questions you had,


    Dan

    Comment

    Working...