header
 
     
 
pixel
pixel pixel

Post form results to same PHP page

For "Contact Us" forms, I prefer to post form results to the same PHP page. I find keeping everything together on the same page is easier to maintain and keep track of (plus I hate directories with too many files). The general structure of the PHP for such a page is as follows:
<?php
if ($_POST)  
    {
    
// display form
?>
<form method='post'>
    <input type='text' name='Name'><br>
    <input type='text' name='Email'><br>
    <input type='submit' value='Submit'>
</form>
<?php
    

else 
    { 
    echo 
"Thanks for your form results";
    
// this code parses the form results and emails them to you
    
$Message ""
    foreach(
$_POST as $key=>$value
        { 
        if (
$value
            
$Message .="$key: $valuern"
        } 
    
// replace 'name@domain.com' with your own email. Your HTML form also needs to have fields Name and Email
    
mail("name@domain.com""Form Results"stripslashes($Message), "From: ".$_POST['Name']." <".$_POST['Email'].">" ); 
    } 
?>

Unhelpful Helpful Rating 3.0 (score out of 5, no. of ratings: 63)
Comments
Comment by sion on 2007-01-26
hello, this didn\'t do much besides email me an empty message titled form results. And the script just displays\"thank you for your form results\" when it\'s called up. I don\'t know much about this stuff so I probably did it wrong.What did I do wrong? I saved the script as formres.php and then even tried having a regular html form post to it because just the script alone seemed ineffectual.But both methods didn\'t work. no big deal.

Post a Comment
Name
Email
(optional)
Comment
RatingUnhelpful Helpful
Security Image* (this is just to prevent spam submissions)
Security Image