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: 124)
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.

Comment by rayallen on 2010-06-04
great!

Comment by James on 2010-09-03
not that i talk on these much he failed to add the ! to the if ($_POST) right now that shows that if you are posting stuff to show the form.. instead of showing us the form to submit.. it should read if (! $_POST)

Comment by ghgh on 2011-11-09
rhfg hgfhh

Comment by sunny Mehta on 2011-07-11
This has been a very significant website indeed. I've acquired a lot of helpful information from your article. Thank you for sharing such relevant topic with us.

Comment by Jose Oliveira on 2010-11-06
asdf

Comment by Jose Oliveira on 2010-11-06
asdf

Comment by kemboy on 2011-03-03
i luv it

Comment by aaa on 2011-04-20
dsrxtred

Comment by cxxzc on 2011-03-15
cxzxczxc

Comment by hemant on 2011-06-04
test

Comment by johnson on 2011-09-29
good

Comment by johnson on 2011-08-25
good

Comment by me on 2011-09-30
How can you create a form that displays erros on same page, for example if you don't complete Name field, it refreshes page(or not) and displays the error under that field.

Comment by fdsgf on 2011-05-31
sdfadfd

Comment by tester on 2011-10-09
test this box ?

Comment by andrei on 2011-05-21
thank you

Comment by discount Michael Jordan Shoes on 2010-09-17
Air Jordan Shoes are always so attractive, good quality can ensure the wearer's feet, not only that, the Michael Jordan Shoes has become a fashion, many young people are very fanatical Air Jordan. High price so many people can realize their dreams. Well now, our products directly from third-party products, so we can provide cheap Jordan Basketball Shoes and New Jordan Shoes, high quality, excellent service, come on, give yourself a choice. We offerAir Jordan 1, Air Jordan 2, Air Jordan 3,Air Jordan 4, Air Jordan 5, Air Jordan 6, Air Jordan 7, Air Jordan 8, Air Jordan 9, Air Jordan 10, Air Jordan 11 and so on!

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