php tutorials, php form validation
Archive Tutorials
Discussion Home
Login / Register

Board List
PHP programming
General

Links
How to save gas 25% or more
Marshall Reddick
Network Marketing Forums
Income Tax Help
Lose 10 Pounds in 3 Weeks
PHP Forum Script
Online Contest

Learn PHP in 47 hours!

Back to PHP programming

Search

validating form mail validating form mail

validating form mail

by oraya on Fri 03/03/2006 03:55 PM

I have a simple script for sending the form mail on to myself (see below) 

<?
$to="me@myself.co.uk";
$from=$_POST['from'];
$subject=$_POST['subject'];
$name=$_POST['name'];
$webaddress=$_POST['webaddress'];
$comments=$_POST['comments'];
$redirect = "thankyou.php";
$message="From:$name\n$email\nWeb Address: $webaddress\n\n$comments";
if(mail($to,$subject,$message,"From: $email\n")) {
  header("Location: $redirect");
} else {
echo "There was a problem sending the mail.";
}
?> 

This works as it should posting the mail to me and redirecting the poster to a thank you page.  I want to include form validation, I did it once before on another site, but for the life of me I can't remember what I did.  I'm very new to php so it's all a learning curb!  If anyone could advise me on how to edit the above to check for name, email, subject and content I'd be greatful. 

Thank you in advance,
Oraya

[Reply] [Back to Top]

validating form entries

by uclaboyz on Sun 03/05/2006 08:18 PM

You can get fancy in checking for a valid email using regular expression and all that crap, but here is the structure on how you would add in your form validation into your current script: 

<?
$to="me@myself.co.uk";
$from=$_POST['from'];
$subject=$_POST['subject'];
$name=$_POST['name'];
$webaddress=$_POST['webaddress'];
$comments=$_POST['comments'];
$redirect = "thankyou.php";
$message="From:$name\n$email\nWeb Address: $webaddress\n\n$comments"; 

// form validation
if(empty($email)) $error_msg="You must enter an email address<br />";
if(empty($name)) $error_msg.="You must enter a name<br />";
if(empty($subject)) $error_msg.="You must enter a subject<br />";
if(empty($comments)) $error_msg.="You must enter your comments<br />"; 

// if no error msg, then proceed, otherwise, redirect to display error messages
if(!empty($error_msg)) header("Location: form_entry.php?error_msg=$error_msg"); 

if(mail($to,$subject,$message,"From: $email\n")) {
header("Location: $redirect");
} else {
echo "There was a problem sending the mail.";
}
?> 

With the code above, I'm assuming the form php file is called form_entry.php.  Since I'm passing $error_msg over to form_entry.php redirect, you can use the variable to display form error messages.  Hope that helps!

[Reply] [Back to Top]

© Copyright 2006-2007.  All Rights Reserved. Site hosted by Small Business Web Design
how to start a blog | parenting tips website | Passive Income | baby websites