PHP Email Form with Validation

Thread Solved

Join Date: Jan 2009
Posts: 68
Reputation: jonow is an unknown quantity at this point 
Solved Threads: 0
jonow jonow is offline Offline
Junior Poster in Training

PHP Email Form with Validation

 
0
  #1
29 Days Ago
Hi, i have a form that when filled out sends and email. I want to be able to validate it, so check that all the fields are filled using PHP. I have found this website: http://www.phpjabbers.com/phpexample.php?eid=27 that i used to validate it but i am not sure how to combine the two PHP scripts to validate and then send. how would i do it?

The reason i am having trouble is both the validation and the email sending PHP require the use of "action" property.

By the way: i dont really know PHP
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 523
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 61
network18 network18 is offline Offline
Posting Pro
 
0
  #2
29 Days Ago
what you trying to do actually is you want to validate your inputs like email id etc before sending the mail.the page you linked up here , shows the use of regular expressions to do that in php.
Now which is your other form, from two forms you mean to say which makes use of the action.
i guess in your first form user inputs his details like email id, name, some body text etc. and in the action of it specified another form, which processes those inputs and sends the mail.
If i guessed right, post your code here, against which fields you want to get validation.
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 68
Reputation: jonow is an unknown quantity at this point 
Solved Threads: 0
jonow jonow is offline Offline
Junior Poster in Training
 
0
  #3
23 Days Ago
Here is my HTML (with PHP validation)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>
  7. Form
  8. </title>
  9. </head>
  10. <body>
  11. <?php
  12. $errName = "";
  13. $errAddress = "";
  14. $errEmail = "";
  15. $errPassport = "";
  16. $errPhone = "";
  17. $errZip = "";
  18. $errDate = "";
  19. $errUser = "";
  20. $errPass = "";
  21. if($_POST["ac"]=="login"){
  22. // First Name
  23. if(preg_match("/^[a-zA-Z -]+$/", $_POST["fname"]) === 0)
  24. $errName1 = '<div class="errtext">Please enter you first name.</div>';
  25. // Last Name
  26. if(preg_match("/^[a-zA-Z -]+$/", $_POST["lname"]) === 0)
  27. $errName2 = '<div class="errtext">Please enter your last name.</div>';
  28. // Email
  29. if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0)
  30. $errEmail = '<div class="errtext">Please enter a valid email.</div>';
  31. // Message
  32. if(preg_match("/^[a-zA-Z -]+$/", $_POST["textarea"]) === 0)
  33. $errName3 = '<div class="errtext">Please enter a message.</div>';
  34. }
  35. ?>
  36. <form method="post" action="<?php $PHP_SELF ?>" id="commentForm" class="contact-form">
  37. <input type="hidden" name="ac" value="login" />
  38. <div class="input-holder-left">
  39. <label for="fname">*First Name:</label>
  40. <div>
  41. <input type="text" name="fname" class="input-short" id="fname" value="<?php echo $_POST["fname"]; ?>" />
  42. <?php if(isset($errName1)) echo $errName1; ?>
  43. </div>
  44. </div>
  45. <div class="input-holder-right">
  46. <label for="lname">*Last Name:</label>
  47. <div>
  48. <input type="text" name="lname" class="input-short" id="lname" value="<?php echo $_POST["lname"]; ?>" />
  49. <?php if(isset($errName2)) echo $errName2; ?>
  50. </div>
  51. </div>
  52. <div class="input-holder">
  53. <label for="email">*Email:</label>
  54. <div>
  55. <input type="text" name="email" class="input-long" id="email" value="<?php echo $_POST["email"]; ?>" />
  56. <?php if(isset($errEmail)) echo $errEmail; ?>
  57. </div>
  58. </div>
  59. <div class="input-holder">
  60. <label for="textarea">*Message:</label>
  61. <div>
  62. <textarea name="textarea" class="textarea-message" id="textarea"><?php echo $_POST["textarea"]; ?></textarea>
  63. <?php if(isset($errName3)) echo $errName3; ?>
  64. </div>
  65. </div>
  66. <div class="input-holder-submit">
  67. <input type="submit" value="Submit" name="submit" class="contact-submit" />
  68. <br />
  69. <br />
  70. <span class="required_field">(*) - Required Field</span>
  71. </div>
  72. </form>
  73. </body>
  74. </html>

And in a separate file the PHP mailer:
  1. <?php
  2. if(isset($_POST['submit'])) {
  3.  
  4. $to = "[email address goes here]";
  5. $subject = "[subject]";
  6. $first_name_field = $_POST['fname'];
  7. $last_name_field = $_POST['lname'];
  8. $email_field = $_POST['email'];
  9. $message = $_POST['message'];
  10.  
  11. $message = "
  12. Name: $first_name_field $last_name_field
  13. Email: $email_field
  14. Message: $message";
  15.  
  16. $headers = 'MIME-Version: 1.0' . "\r\n";
  17. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  18.  
  19. // Additional headers
  20. $headers .= 'To: <[email address goes here]>' . "\r\n";
  21. $headers .= '[From] <[email address goes here]>' . "\r\n";
  22.  
  23. // Mail it
  24. mail($to, $subject, $message, $headers);
  25.  
  26. include("[Result page]");
  27. ini_set("sendmail_from","[Send from]");
  28. ini_set("SMTP","[mail server]");
  29. mail($to, $subject, $message, $headers);
  30.  
  31. } else {
  32.  
  33. echo "Error!";
  34.  
  35. }
  36. ?>
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 47
Reputation: EvolutionFallen is an unknown quantity at this point 
Solved Threads: 8
EvolutionFallen EvolutionFallen is offline Offline
Light Poster
 
1
  #4
22 Days Ago
You have a couple of problems here:
1) Your form action is set to $PHP_SELF , which I don't see being initialized anywhere here. So unless you have another file somewhere that you include with your first file, you need to set $PHP_SELF = $_SERVER['PHP_SELF'];
2) $PHP_SELF (once set appropriately) will forward your form information to the same page you're on already, so your form information never actually gets sent to the page with the second set of code posted. One thing you can do to solve this is to move the code from your second page into your first page, and rearrange the PHP part so it looks like this:

  1. <?php
  2.  
  3. if(isset($_POST['submit'])) {
  4.  
  5. if($_POST["ac"]=="login"){
  6. // First Name
  7. if(preg_match("/^[a-zA-Z -]+$/", $_POST["fname"]) === 0)
  8. $errName1 = '<div class="errtext">Please enter you first name.</div>';
  9. // Last Name
  10. if(preg_match("/^[a-zA-Z -]+$/", $_POST["lname"]) === 0)
  11. $errName2 = '<div class="errtext">Please enter your last name.</div>';
  12. // Email
  13. if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0)
  14. $errEmail = '<div class="errtext">Please enter a valid email.</div>';
  15. // Message
  16. if(preg_match("/^[a-zA-Z -]+$/", $_POST["textarea"]) === 0)
  17. $errName3 = '<div class="errtext">Please enter a message.</div>';
  18.  
  19.  
  20. $to = "[email address goes here]";
  21. $subject = "[subject]";
  22. $first_name_field = $_POST['fname'];
  23. $last_name_field = $_POST['lname'];
  24. $email_field = $_POST['email'];
  25. $message = $_POST['message'];
  26.  
  27. $message = "
  28. Name: $first_name_field $last_name_field
  29. Email: $email_field
  30. Message: $message";
  31.  
  32. $headers = 'MIME-Version: 1.0' . "\r\n";
  33. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  34.  
  35. // Additional headers
  36. $headers .= 'To: <[email address goes here]>' . "\r\n";
  37. $headers .= '[From] <[email address goes here]>' . "\r\n";
  38.  
  39. // Mail it
  40. mail($to, $subject, $message, $headers);
  41.  
  42. include("[Result page]");
  43. ini_set("sendmail_from","[Send from]");
  44. ini_set("SMTP","[mail server]");
  45. mail($to, $subject, $message, $headers);
  46.  
  47. }
  48. } else {
  49.  
  50. echo "Error!";
  51. }
  52.  
  53. ?>
Last edited by EvolutionFallen; 22 Days Ago at 10:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 68
Reputation: jonow is an unknown quantity at this point 
Solved Threads: 0
jonow jonow is offline Offline
Junior Poster in Training
 
0
  #5
22 Days Ago
in the place were it says $PHP_SELF it is supposed to be the URL to the PHP mailer. I don't know why it was like that.

What exactly am i supposed to do? im not sure if i said it above or not but i am dont really know PHP
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 47
Reputation: EvolutionFallen is an unknown quantity at this point 
Solved Threads: 8
EvolutionFallen EvolutionFallen is offline Offline
Light Poster
 
0
  #6
22 Days Ago
You can put the URL to the second file instead of $PHP_SELF, that will work, but then you'll bypass your validation. So your options are either to put the second file into the first, as I showed above, or to put the validation code from the first into the second.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 68
Reputation: jonow is an unknown quantity at this point 
Solved Threads: 0
jonow jonow is offline Offline
Junior Poster in Training
 
0
  #7
22 Days Ago
Originally Posted by EvolutionFallen View Post
You can put the URL to the second file instead of $PHP_SELF, that will work, but then you'll bypass your validation. So your options are either to put the second file into the first, as I showed above, or to put the validation code from the first into the second.
I did what you did above but it did not work
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 47
Reputation: EvolutionFallen is an unknown quantity at this point 
Solved Threads: 8
EvolutionFallen EvolutionFallen is offline Offline
Light Poster
 
0
  #8
22 Days Ago
My apologies. I misunderstood the code before, and how the error validation is working.
Try this. I didn't test it myself, but it should work. If it still doesn't, let me know what's going wrong. Also, make sure to read the comments I added on lines 13-14, 38, 39, 60, and 62:

  1. <?php
  2.  
  3. $PHP_SELF = $_SERVER['PHP_SELF'];
  4. $errName1 = "";
  5. $errName2 = "";
  6. $errEmail = "";
  7. $errMessage = "";
  8.  
  9. if(isset($_POST['submit'])) {
  10.  
  11. if($_POST["ac"]=="login"){
  12.  
  13. $FORMOK = TRUE; // $FORMOK acts as a flag. If you enter any of the conditionals below,
  14. // it gets set to FALSE, and the e-mail will not be sent.
  15.  
  16. // First Name
  17. if(preg_match("/^[a-zA-Z -]+$/", $_POST["fname"]) === 0) {
  18. $errName1 = '<div class="errtext">Please enter you first name.</div>';
  19. $FORMOK = FALSE;
  20. }
  21. // Last Name
  22. if(preg_match("/^[a-zA-Z -]+$/", $_POST["lname"]) === 0) {
  23. $errName2 = '<div class="errtext">Please enter your last name.</div>';
  24. $FORMOK = FALSE;
  25. }
  26. // Email
  27. if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0) {
  28. $errEmail = '<div class="errtext">Please enter a valid email.</div>';
  29. $FORMOK = FALSE;
  30. }
  31. // Message
  32. if(preg_match("/^[a-zA-Z -]+$/", $_POST["textarea"]) === 0) {
  33. $errMessage = '<div class="errtext">Please enter a message.</div>';
  34. $FORMOK = FALSE;
  35. }
  36.  
  37. if($FORMOK) {
  38. $to = "[email address goes here]"; // Replace [] with your e-mail address -- make sure to delete the brackets too
  39. $subject = "[subject]"; // Same thing for subject
  40. $first_name_field = $_POST['fname'];
  41. $last_name_field = $_POST['lname'];
  42. $email_field = $_POST['email'];
  43. $message = $_POST['message'];
  44.  
  45. $message = "
  46. Name: $first_name_field $last_name_field
  47. Email: $email_field
  48. Message: $message";
  49.  
  50. $headers = 'MIME-Version: 1.0' . "\r\n";
  51. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  52.  
  53. // Additional headers
  54. $headers .= 'To: <[email address goes here]>' . "\r\n";
  55. $headers .= '[From] <[email address goes here]>' . "\r\n";
  56.  
  57. // Mail it
  58. mail($to, $subject, $message, $headers);
  59.  
  60. include("[Result page]"); // Replace [] with the URL of your result page -- again, make sure to delete the brackets too.
  61.  
  62. // I have no idea what these next 3 lines are for. You may just want to get rid of them.
  63. ini_set("sendmail_from","[Send from]");
  64. ini_set("SMTP","[mail server]");
  65. mail($to, $subject, $message, $headers);
  66.  
  67. } else {
  68. echo "Error!";
  69. }
  70. }
  71. }
  72. ?>
  73. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  74. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  75. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en">
  76. <head>
  77. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  78. <title>
  79. Form
  80. </title>
  81. </head>
  82. <body>
  83. <form method="post" action="<?php $PHP_SELF ?>" id="commentForm" class="contact-form">
  84. <input type="hidden" name="ac" value="login" />
  85. <div class="input-holder-left">
  86. <label for="fname">*First Name:</label>
  87. <div>
  88. <input type="text" name="fname" class="input-short" id="fname" value="<?php echo $_POST["fname"]; ?>" />
  89. <?php if(isset($errName1)) echo $errName1; ?>
  90. </div>
  91. </div>
  92. <div class="input-holder-right">
  93. <label for="lname">*Last Name:</label>
  94. <div>
  95. <input type="text" name="lname" class="input-short" id="lname" value="<?php echo $_POST["lname"]; ?>" />
  96. <?php if(isset($errName2)) echo $errName2; ?>
  97. </div>
  98. </div>
  99. <div class="input-holder">
  100. <label for="email">*Email:</label>
  101. <div>
  102. <input type="text" name="email" class="input-long" id="email" value="<?php echo $_POST["email"]; ?>" />
  103. <?php if(isset($errEmail)) echo $errEmail; ?>
  104. </div>
  105. </div>
  106. <div class="input-holder">
  107. <label for="textarea">*Message:</label>
  108. <div>
  109. <textarea name="textarea" class="textarea-message" id="textarea"><?php echo $_POST["textarea"]; ?></textarea>
  110. <?php if(isset($errMessage)) echo $errMessage; ?>
  111. </div>
  112. </div>
  113. <div class="input-holder-submit">
  114. <input type="submit" value="Submit" name="submit" class="contact-submit" />
  115. <br />
  116. <br />
  117. <span class="required_field">(*) - Required Field</span>
  118. </div>
  119. </form>
  120. </body>
  121. </html>
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 68
Reputation: jonow is an unknown quantity at this point 
Solved Threads: 0
jonow jonow is offline Offline
Junior Poster in Training
 
0
  #9
22 Days Ago
Can i just say YOU ARE GOD!

But there is one small problem. The result page you get to is imputed into the page. I want it to send you to a new page. How do I do that? It says include("contact_success.php"); how do i make it so it sends you to that page?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 47
Reputation: EvolutionFallen is an unknown quantity at this point 
Solved Threads: 8
EvolutionFallen EvolutionFallen is offline Offline
Light Poster
 
0
  #10
22 Days Ago
You have two options:
1) You can keep the include there, and just make the page exit afterward, like so:
  1. include("contact_success.php");
  2. exit();
2) Alternatively, you can use a PHP redirect, which is what I usually do. Get rid of include("contact_success.php"); and instead of it place this line:
  1. header("Location: contact_success.php");

Glad to help =)

- EF
Last edited by EvolutionFallen; 22 Days Ago at 3:12 am.
Reply With Quote Quick reply to this message  
Reply

Tags
action, email, form, php, trouble, validation

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC