| | |
PHP Email Form with Validation
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 68
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2009
Posts: 523
Reputation:
Solved Threads: 61
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.
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
follow me on twitter
•
•
Join Date: Jan 2009
Posts: 68
Reputation:
Solved Threads: 0
0
#3 23 Days Ago
Here is my HTML (with PHP validation)
And in a separate file the PHP mailer:
PHP Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Form </title> </head> <body> <?php $errName = ""; $errAddress = ""; $errEmail = ""; $errPassport = ""; $errPhone = ""; $errZip = ""; $errDate = ""; $errUser = ""; $errPass = ""; if($_POST["ac"]=="login"){ // First Name if(preg_match("/^[a-zA-Z -]+$/", $_POST["fname"]) === 0) $errName1 = '<div class="errtext">Please enter you first name.</div>'; // Last Name if(preg_match("/^[a-zA-Z -]+$/", $_POST["lname"]) === 0) $errName2 = '<div class="errtext">Please enter your last name.</div>'; if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0) $errEmail = '<div class="errtext">Please enter a valid email.</div>'; // Message if(preg_match("/^[a-zA-Z -]+$/", $_POST["textarea"]) === 0) $errName3 = '<div class="errtext">Please enter a message.</div>'; } ?> <form method="post" action="<?php $PHP_SELF ?>" id="commentForm" class="contact-form"> <input type="hidden" name="ac" value="login" /> <div class="input-holder-left"> <label for="fname">*First Name:</label> <div> <input type="text" name="fname" class="input-short" id="fname" value="<?php echo $_POST["fname"]; ?>" /> <?php if(isset($errName1)) echo $errName1; ?> </div> </div> <div class="input-holder-right"> <label for="lname">*Last Name:</label> <div> <input type="text" name="lname" class="input-short" id="lname" value="<?php echo $_POST["lname"]; ?>" /> <?php if(isset($errName2)) echo $errName2; ?> </div> </div> <div class="input-holder"> <label for="email">*Email:</label> <div> <input type="text" name="email" class="input-long" id="email" value="<?php echo $_POST["email"]; ?>" /> <?php if(isset($errEmail)) echo $errEmail; ?> </div> </div> <div class="input-holder"> <label for="textarea">*Message:</label> <div> <textarea name="textarea" class="textarea-message" id="textarea"><?php echo $_POST["textarea"]; ?></textarea> <?php if(isset($errName3)) echo $errName3; ?> </div> </div> <div class="input-holder-submit"> <input type="submit" value="Submit" name="submit" class="contact-submit" /> <br /> <br /> <span class="required_field">(*) - Required Field</span> </div> </form> </body> </html>
And in a separate file the PHP mailer:
PHP Syntax (Toggle Plain Text)
<?php if(isset($_POST['submit'])) { $to = "[email address goes here]"; $subject = "[subject]"; $first_name_field = $_POST['fname']; $last_name_field = $_POST['lname']; $email_field = $_POST['email']; $message = $_POST['message']; $message = " Name: $first_name_field $last_name_field Email: $email_field Message: $message"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: <[email address goes here]>' . "\r\n"; $headers .= '[From] <[email address goes here]>' . "\r\n"; // Mail it mail($to, $subject, $message, $headers); include("[Result page]"); ini_set("sendmail_from","[Send from]"); ini_set("SMTP","[mail server]"); mail($to, $subject, $message, $headers); } else { echo "Error!"; } ?>
•
•
Join Date: Aug 2009
Posts: 47
Reputation:
Solved Threads: 8
1
#4 22 Days Ago
You have a couple of problems here:
1) Your form action is set to
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) 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:
php Syntax (Toggle Plain Text)
<?php if(isset($_POST['submit'])) { if($_POST["ac"]=="login"){ // First Name if(preg_match("/^[a-zA-Z -]+$/", $_POST["fname"]) === 0) $errName1 = '<div class="errtext">Please enter you first name.</div>'; // Last Name if(preg_match("/^[a-zA-Z -]+$/", $_POST["lname"]) === 0) $errName2 = '<div class="errtext">Please enter your last name.</div>'; if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0) $errEmail = '<div class="errtext">Please enter a valid email.</div>'; // Message if(preg_match("/^[a-zA-Z -]+$/", $_POST["textarea"]) === 0) $errName3 = '<div class="errtext">Please enter a message.</div>'; $to = "[email address goes here]"; $subject = "[subject]"; $first_name_field = $_POST['fname']; $last_name_field = $_POST['lname']; $email_field = $_POST['email']; $message = $_POST['message']; $message = " Name: $first_name_field $last_name_field Email: $email_field Message: $message"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: <[email address goes here]>' . "\r\n"; $headers .= '[From] <[email address goes here]>' . "\r\n"; // Mail it mail($to, $subject, $message, $headers); include("[Result page]"); ini_set("sendmail_from","[Send from]"); ini_set("SMTP","[mail server]"); mail($to, $subject, $message, $headers); } } else { echo "Error!"; } ?>
Last edited by EvolutionFallen; 22 Days Ago at 10:53 pm.
•
•
Join Date: Jan 2009
Posts: 68
Reputation:
Solved Threads: 0
0
#7 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.
•
•
Join Date: Aug 2009
Posts: 47
Reputation:
Solved Threads: 8
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:
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:
php Syntax (Toggle Plain Text)
<?php $PHP_SELF = $_SERVER['PHP_SELF']; $errName1 = ""; $errName2 = ""; $errEmail = ""; $errMessage = ""; if(isset($_POST['submit'])) { if($_POST["ac"]=="login"){ $FORMOK = TRUE; // $FORMOK acts as a flag. If you enter any of the conditionals below, // it gets set to FALSE, and the e-mail will not be sent. // First Name if(preg_match("/^[a-zA-Z -]+$/", $_POST["fname"]) === 0) { $errName1 = '<div class="errtext">Please enter you first name.</div>'; $FORMOK = FALSE; } // Last Name if(preg_match("/^[a-zA-Z -]+$/", $_POST["lname"]) === 0) { $errName2 = '<div class="errtext">Please enter your last name.</div>'; $FORMOK = FALSE; } if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0) { $errEmail = '<div class="errtext">Please enter a valid email.</div>'; $FORMOK = FALSE; } // Message if(preg_match("/^[a-zA-Z -]+$/", $_POST["textarea"]) === 0) { $errMessage = '<div class="errtext">Please enter a message.</div>'; $FORMOK = FALSE; } if($FORMOK) { $to = "[email address goes here]"; // Replace [] with your e-mail address -- make sure to delete the brackets too $subject = "[subject]"; // Same thing for subject $first_name_field = $_POST['fname']; $last_name_field = $_POST['lname']; $email_field = $_POST['email']; $message = $_POST['message']; $message = " Name: $first_name_field $last_name_field Email: $email_field Message: $message"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: <[email address goes here]>' . "\r\n"; $headers .= '[From] <[email address goes here]>' . "\r\n"; // Mail it mail($to, $subject, $message, $headers); include("[Result page]"); // Replace [] with the URL of your result page -- again, make sure to delete the brackets too. // I have no idea what these next 3 lines are for. You may just want to get rid of them. ini_set("sendmail_from","[Send from]"); ini_set("SMTP","[mail server]"); mail($to, $subject, $message, $headers); } else { echo "Error!"; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Form </title> </head> <body> <form method="post" action="<?php $PHP_SELF ?>" id="commentForm" class="contact-form"> <input type="hidden" name="ac" value="login" /> <div class="input-holder-left"> <label for="fname">*First Name:</label> <div> <input type="text" name="fname" class="input-short" id="fname" value="<?php echo $_POST["fname"]; ?>" /> <?php if(isset($errName1)) echo $errName1; ?> </div> </div> <div class="input-holder-right"> <label for="lname">*Last Name:</label> <div> <input type="text" name="lname" class="input-short" id="lname" value="<?php echo $_POST["lname"]; ?>" /> <?php if(isset($errName2)) echo $errName2; ?> </div> </div> <div class="input-holder"> <label for="email">*Email:</label> <div> <input type="text" name="email" class="input-long" id="email" value="<?php echo $_POST["email"]; ?>" /> <?php if(isset($errEmail)) echo $errEmail; ?> </div> </div> <div class="input-holder"> <label for="textarea">*Message:</label> <div> <textarea name="textarea" class="textarea-message" id="textarea"><?php echo $_POST["textarea"]; ?></textarea> <?php if(isset($errMessage)) echo $errMessage; ?> </div> </div> <div class="input-holder-submit"> <input type="submit" value="Submit" name="submit" class="contact-submit" /> <br /> <br /> <span class="required_field">(*) - Required Field</span> </div> </form> </body> </html>
•
•
Join Date: Aug 2009
Posts: 47
Reputation:
Solved Threads: 8
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:
2) Alternatively, you can use a PHP redirect, which is what I usually do. Get rid of
Glad to help =)
- EF
1) You can keep the include there, and just make the page exit afterward, like so:
php Syntax (Toggle Plain Text)
include("contact_success.php"); exit();
include("contact_success.php"); and instead of it place this line: php Syntax (Toggle Plain Text)
header("Location: contact_success.php");
Glad to help =)
- EF
Last edited by EvolutionFallen; 22 Days Ago at 3:12 am.
![]() |
Similar Threads
- Code Snippet: PHP Email Address validation through SMTP (PHP)
- A different form validation (JavaScript / DHTML / AJAX)
- email and form validation (JavaScript / DHTML / AJAX)
- php regex for form validation (PHP)
- Error in php form validation (PHP)
- PHP Form Validation ??? (PHP)
- PHP Mail Form Validation - Help Please? (PHP)
- Dreamweaver php form validation and redirect (PHP)
- PHP Contact Form Help (PHP)
- Please help with email form script (PHP)
Other Threads in the PHP Forum
- Previous Thread: Deleting rows
- Next Thread: how ro grab discription from oteh website in php?
| Thread Tools | Search this Thread |
6 acceptableuse action ajax api array asp basic beginner broken browser c# c++ checkbox class cms code confidentiality curl daniweb database display ecommerce electronicdiscovery email encryption eu files firefox flash flex form forms gmail google government hack hosting html image include insert internet java javascript jquery lamp limit link linux login mail malware menu microsoft mobile mozilla multiple mysql news number onecare oop open opensource opinion password phishing php post privacy query retention script search security sharepoint simple sms spam spamming sql survey textbox thunderbird trends tutorial twitter upload validation validator video virus visual web webmail window yahoo youtube zend






