My php Form tutorial

WinGate

New Member
Messages
98
Reaction score
0
Points
0
You will need 2 files. One will be a HTML page with a form on it. The other file will be pure php script that will phase the information from the HTML formant send it to you as an E-Mail.
auto_art_429.gif

Once the Form has been submitted a "Thanks we have your Message" message will appear to give the user feedback that the form has been accepted. The form will also clear itself ready for a new massage.
auto_art_427.gif


This is your form page. You can call it what ever you want. In this example I call this file index.php

Code:
[FONT=Courier]<?php
session_start(); 
    [/FONT][FONT=Courier][COLOR=#008000]// starts session that links the $_SESSION variable.. 
[/COLOR]
?>
<form action="mail_form.php" method="post">
<font face="Arial" size="3">
<?php
echo @$_SESSION['message'];  
    [/FONT][FONT=Courier][COLOR=#008000]// this will insert "Thanks we have your Message" from mail_form.php

[/COLOR]unset($_SESSION['message']);    
    [/FONT][FONT=Courier][COLOR=#008000]// clears $_SESSION['message'] variable

[/COLOR]?>
</font>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr style="font-family: Arial;">
<input name="title" value="Web Developers > Web Languages > php > Mail to Form > " type="hidden">
<input type="hidden" name="returnto" value="<?php echo $_SERVER['PHP_SELF']; ?>">
</tr>
<tr>
<td style="width: 1034px;"><font face="Arial" size="2">Comments</font><br>
<textarea cols="60" rows="3" name="comments"></textarea><br>
<input value="Submit" type="submit"><input type="reset" value="Reset" name="Reset"></td>
</tr>
</tbody>
</table>
&nbsp;</form>[/FONT]

This is your php script. Saved as mail_form.php

Code:
[FONT=Courier]<?session_start();   
[COLOR=#008000]   // starts session that links the $_SESSION variable.. [/COLOR][/FONT]               
[FONT=Courier]$comments=$_POST['comments'];  
[/FONT][FONT=Courier][COLOR=#008000]   // gets the variable 'comment' from 
[/COLOR]
$to= your_return_email_address@woosh.co.nz; 
[COLOR=#008000]   // put your E-Mail address here. [/COLOR][/FONT]
               [FONT=Courier]$message="Comment:\n $comments"; 
    [/FONT][FONT=Courier][COLOR=#008000]//   arranges data into $message ready to 
    //   be posted to you. 
    //   [/COLOR][COLOR=#008000]\n is a line return[/COLOR][/FONT]
               [FONT=Courier]mail($to,"Comments From Your Site",$message); 
[/FONT][FONT=Courier][COLOR=#008000]    //  mail () will E-Mail the data to you 
    //  $to:  is the E-Mail address to sent form data to
    //  "Comments From Your Site" Subject of E-Mail
    //  $message: The actual message you will receive.                     [/COLOR][/FONT]
               [FONT=Courier]$_SESSION['message'] = "Thanks we have your Message";
[/FONT][FONT=Courier]    [COLOR=#008000]// Message sent back to index.php to let user know you have received
    // input... [/COLOR] 
header("Location: " . $_POST['returnto']);
exit;
?>[/FONT]

And here is the example link:
Code:
http://udopage.com/examples/forms/simple.php
 
Top