View Full Version : Form Validation
Im looking to create a form with the following fields, with results emailed to specific email.:
First Name:
Last Name:
Address:
City:
State:
Zip:
Phone:
Email:
Seminar Location:
Seminar Date:
Seminar Time:
Im having problems making the "error" messages appear on the page, rather than a seperate page or an alert window.
Any help would be great.
_Aerospace_Eng_
05-01-2006, 09:31 PM
Can you use php? I have a php form that displays the error messages on the same page. http://www.htmlforums.com/showthread.php?t=73865
I can use PHP. I will give your form a try. Your referring to this one:
<div id="content">
<?php
if(isset($_POST['submit']) && $_POST['submit'] == 'Submit') //if form has been submitted
{
//for now:
//get all the data from the form
$name= $_POST['name'];
$email = $_POST['email'];
$usersubject = $_POST['subject'];
$comment = stripslashes($_POST['comment']);
$lang = $_POST['lang'];
$graphics = $_POST['graphics'];
$from="someemail@blah.com"; //this is what will appear in the from part in your email
$errors = '';
if($name == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"name\">name</label>.</span><br />";
}
if($email == '')
{
$error .= "<span class=\"error\">You must enter an <label for=\"email\">e-mail</label> address.</span><br />";
}
elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$", $email)) //if email entered, verify format
{
$error .= "<span class=\"error\">Your <label for=\"email\">email</label> is not of valid format.</span><br />";
}
if($comment == '')
{
$error .= "<span class=\"error\">You must enter some <label for=\"comment\">comments</label>.</span><br />";
}
if($error == '') //If no errors
{
$sendTo = "Paul <someone@blah.com>"; //this will be the address that the mail gets sent to
$subject = "Mail from PR Designz";
$body = "You have received the following from the web based contact form:\r\n";
$body .= "---------------------------------------------------------------\r\n";
$body .= "Subject: Mail from Site\r\n";
$body .= "From: ".$email."\r\n";
$body .= "IP Address: ".$_SERVER['REMOTE_ADDR']."\r\n";
$body .= "Grahpics: ".$graphics."\r\n";
$body .= "Language: ".$lang."\r\n";
$body .= "Questions/Comments:\r\n";
$body .= $comment ."\r\n";
$body .= "---------------------------------------------------------------\r\n";
$headers = 'From: '.$from."\r\n" .
'Reply-To: '.$email;
if(mail($sendTo, $subject, $body, $headers))
{
$success="<div class=\"error centertext\">E-mail sucessfully sent.<br />Thank you for contacting me.</div>";
$name='';
$email='';
$comment='';
}
else
{
$error = "<div class=\"error\"><span class=\"error\">Unfortunately, we were unable to send your e-mail. Please try again.</span></div>";
exit();
}
}
}
?>
<p class="centertext">Below is a contact form that you can use to submit your comments.<br />All fields are required.<br /><br /></p>
<?php
if(!empty($error))
{
echo "<div class=\"error\">\n";
echo "<h4>Uh oh! An error occurred...</h4>";
echo $error;
echo "</div>\n";
}
if(!empty($success))
{
echo $success;
}
?>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']?>">
<fieldset id="emailform">
<legend id="legend">Contact Me</legend>
<label for="name">Name</label>
<input class="text" type="text" size="50" name="name" id="name" value="<?php echo $name; ?>" maxlength="50" />
<label for="email">Email</label>
<input class="text" type="text" size="50" name="email" id="email" value="<?php echo $email; ?>" maxlength="100" />
<p class="centertext"><input type="checkbox" onclick="request()" class="webserv" name="webserv" id="webserv" /><label for="webserv" class="webserv">Do you require web services?</label></p>
<div id="web1">What language will the site need to be coded in?
<input class="text" type="text" size="50" name="lang" id="lang" maxlength="50" /></div>
<div id="web2">Will graphics be provided?<br />
<select class="text" name="graphics">
<option value="No">No</option>
<option value="Yes">Yes</option>
</select></div>
<label for="comment">Questions/Comments</label>
<textarea class="text2" name="comment" id="comment" cols="5" rows="4"><?php echo $comment; ?></textarea>
<div class="input"><input class="buttons" type="submit" name="submit" value="Submit" /> <input class="buttons" type="reset" value="Reset" /></div>
</fieldset>
</form>
</div>
<!--End Content-->
_Aerospace_Eng_
05-01-2006, 11:38 PM
Yeah. Hmm that would explain the recent spam. I left my email addy in there.
Pegasus
05-02-2006, 09:52 AM
I think that might fix it.
Peg
I edited it slightly and it doesnt send the email:
<div id="content"> <?php if(isset($_POST['submit']) && $_POST['submit'] == 'Submit') //if form has been submitted { //for now: //get all the data from the form $name= $_POST['name']; $email = $_POST['email']; $usersubject = $_POST['subject']; $comment = stripslashes($_POST['comment']); $lang = $_POST['lang']; $graphics = $_POST['graphics']; $from="rsvp@positscience.com"; //this is what will appear in the from part in your email $errors = ''; if($name == '') { $error .= "<span class=\"error\">You must enter your <label for=\"name\">name</label>.</span><br />"; } if($email == '') { $error .= "<span class=\"error\">You must enter an <label for=\"email\">e-mail</label> address.</span><br />"; } elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$", $email)) //if email entered, verify format { $error .= "<span class=\"error\">Your <label for=\"email\">email</label> is not of valid format.</span><br />"; } if($comment == '') { $error .= "<span class=\"error\">You must enter some <label for=\"comment\">comments</label>.</span><br />"; } if($error == '') //If no errors { $sendTo = "RSVP <mac@positscience.com>"; //this will be the address that the mail gets sent to $subject = "RSVP Response"; $body = "You have received the following from the web based contact form:\r\n"; $body .= "---------------------------------------------------------------\r\n"; $body .= "Subject: Mail from Site\r\n"; $body .= "From: ".$email."\r\n"; $body .= "IP Address: ".$_SERVER['REMOTE_ADDR']."\r\n"; $body .= "Grahpics: ".$graphics."\r\n"; $body .= "Language: ".$lang."\r\n"; $body .= "Questions/Comments:\r\n"; $body .= $comment ."\r\n"; $body .= "---------------------------------------------------------------\r\n"; $headers = 'From: '.$from."\r\n" . 'Reply-To: '.$email; if(mail($sendTo, $subject, $body, $headers)) { $success="<div class=\"error centertext\">E-mail sucessfully sent.<br />Thank you for contacting me.</div>"; $name=''; $email=''; $comment=''; } else { $error = "<div class=\"error\"><span class=\"error\">Unfortunately, we were unable to send your e-mail. Please try again.</span></div>"; exit(); } } } ?> <p class="centertext">Below is a contact form that you can use to submit your comments.<br />All fields are required.<br />
<?php if(!empty($error)) { echo "<div class=\"error\">\n"; echo "<h4>Uh oh! An error occurred...</h4>"; echo $error; echo "</div>\n"; } if(!empty($success)) { echo $success; } ?>
</p>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']?>"> <fieldset id="emailform"> <legend id="legend">Contact Me</legend> <label for="name">Name</label> <input class="text" type="text" size="50" name="name" id="name" value="<?php echo $name; ?>" maxlength="50" /> <label for="email">Email</label> <input class="text" type="text" size="50" name="email" id="email" value="<?php echo $email; ?>" maxlength="100" /> <p class="centertext"><input type="checkbox" onclick="request()" class="webserv" name="webserv" id="webserv" /><label for="webserv" class="webserv">Do you require web services?</label></p> <div id="web1">What language will the site need to be coded in? <input class="text" type="text" size="50" name="lang" id="lang" maxlength="50" /></div> <div id="web2">Will graphics be provided?<br /> <select class="text" name="graphics"> <option value="No">No</option> <option value="Yes">Yes</option> </select></div> <label for="comment">Questions/Comments</label> <textarea class="text2" name="comment" id="comment" cols="5" rows="4"><?php echo $comment; ?></textarea> <div class="input"><input class="buttons" type="submit" name="submit" value="Submit" /> <input class="buttons" type="reset" value="Reset" /></div> </fieldset> </form> </div> <!--End Content-->
AE....nevermind.
I got it to work perfectly.
I appreciate the headups on the php script. Nice piece of work.
Thanks once again!
-Mac
Aerospace...
One quick question. I want the form submission to go go to a "thank you" page, rather than display the success message on the page. I tried doing:
location.replace("thankyou.php");
but, as you know, Im not a JS expert and had trouble getting it to work.
thanks, Mac
_Aerospace_Eng_
05-03-2006, 01:40 AM
Post the current script you are using. Though you should be able to just change this
if(mail($sendTo, $subject, $body, $headers))
{
$success="<div class=\"error centertext\">E-mail sucessfully sent.<br />Thank you for contacting me.</div>";
$name='';
$email='';
$comment='';
}
to this
if(mail($sendTo, $subject, $body, $headers))
{
header("Location: thankyou.php");
}
AE,
This doesnt seem to work:
<?php
if(isset($_POST['submit']) && $_POST['submit'] == 'Submit') //if form has been submitted
{
//for now:
//get all the data from the form
$firstname= $_POST['firstname'];
$lastname= $_POST['lastname'];
$address= $_POST['address'];
$city= $_POST['city'];
$state= $_POST['state'];
$zipcode= $_POST['zipcode'];
$phone= $_POST['phone'];
$email = $_POST['email'];
$semloc= $_POST['semloc'];
$semdate= $_POST['semdate'];
$semtime= $_POST['semtime'];
$brainfitnessnews= $_POST['brainfitnessnews'];
$usersubject = $_POST['subject'];
$from="rsvp@positscience.com"; //this is what will appear in the from part in your email
$errors = '';
if($firstname == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"firstname\">First Name</label>.</span><br />";
}
if($lastname == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"lastname\">Last Name</label>.</span><br />";
}
if($address == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"address\">Address</label>.</span><br />";
}
if($city == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"city\">City</label>.</span><br />";
}
if($state == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"state\">State</label>.</span><br />";
}
if($zipcode == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"zipcode\">Zip Code</label>.</span><br />";
}
if($phone == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"phone\">Phone Number</label>.</span><br />";
}
if($email == '')
{
$error .= "<span class=\"error\">You must enter an <label for=\"email\">e-mail</label> address.</span><br />";
}
elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$", $email)) //if email entered, verify format
{
$error .= "<span class=\"error\">Your <label for=\"email\">email</label> is not of valid format.</span><br />";
}
if($semloc == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"name\">Seminar Location</label>.</span><br />";
}
if($semdate == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"name\">Seminar Date</label>.</span><br />";
}
if($semtime == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"name\">Seminar Time</label>.</span><br />";
}
if($error == '') //If no errors
{
$sendTo = "Online Seminar <rsvp@positscience.com>"; //this will be the address that the mail gets sent to
$subject = "RSVP Email";
$body = "You have received the following information from a RSVP submission:\r\n";
$body .= "---------------------------------------------------------------\r\n";
$body .= "Subject: Mail from Site\r\n";
$body .= "From: ".$email."\r\n";
$body .= "IP Address: ".$_SERVER['REMOTE_ADDR']."\r\n";
$body .= "FirstName: ".$firstname."\r\n";
$body .= "LastName: ".$lastname."\r\n";
$body .= "Address: ".$address."\r\n";
$body .= "City: ".$city."\r\n";
$body .= "State: ".$state."\r\n";
$body .= "ZipCode: ".$zipcode."\r\n";
$body .= "Phone: ".$phone."\r\n";
$body .= "Email: ".$email."\r\n";
$body .= "SemLoc: ".$semloc."\r\n";
$body .= "SemDate: ".$semdate."\r\n";
$body .= "SemTime: ".$semtime."\r\n";
$body .= "BrainFitnessNews: ".$brainfitnessnews."\r\n";
$body .= "---------------------------------------------------------------\r\n";
$headers = 'From: '.$from."\r\n" .
'Reply-To: '.$email;
if(mail($sendTo, $subject, $body, $headers))
{
header("Location: thankyou.php");
}
else
{
$error = "<div class=\"error\"><span class=\"error\">Unfortunately, we were unable to send your e-mail. Please try again.</span></div>";
}
}
}
?>
Any suggestions?
_Aerospace_Eng_
05-03-2006, 02:05 PM
Are you getting any errors? Something like headers already sent?
_Aerospace_Eng_
05-03-2006, 02:14 PM
I just tried it on my site and it works fine. In order for that to work properly ALL of the php needs to be at the top of your page before your doctype.
my bad, that was the problem. Thanks again. Works perfect now.
AerospaceE,
With this form piece, Im trying to add a dropdown menu and have the form still work, but I get an error message for each of the 3 dropdowns. Here is the code:
Top PHP Code:
<?php
if(isset($_POST['submit']) && $_POST['submit'] == 'Submit') //if form has been submitted
{
//for now:
//get all the data from the form
$firstname= $_POST['firstname'];
$lastname= $_POST['lastname'];
$address= $_POST['address'];
$city= $_POST['city'];
$state= $_POST['state'];
$zipcode= $_POST['zipcode'];
$phone= $_POST['phone'];
$email = $_POST['email'];
$semloc= $_POST['semloc'];
$semdate= $_POST['semdate'];
$semtime= $_POST['semtime'];
$agerange= $_POST['agerange'];
$whyinterested= $_POST['whyinterested'];
$howconvenient= $_POST['howconvenient'];
$idealtime= $_POST['idealtime'];
$brainfitnessnews= $_POST['brainfitnessnews'];
$usersubject = $_POST['subject'];
$from="rsvp@positscience.com"; //this is what will appear in the from part in your email
$errors = '';
if($firstname == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"firstname\">First Name</label>.</span><br />";
}
if($lastname == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"lastname\">Last Name</label>.</span><br />";
}
if($address == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"address\">Address</label>.</span><br />";
}
if($city == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"city\">City</label>.</span><br />";
}
if($state == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"state\">State</label>.</span><br />";
}
if($zipcode == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"zipcode\">Zip Code</label>.</span><br />";
}
if($phone == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"phone\">Phone Number</label>.</span><br />";
}
if($email == '')
{
$error .= "<span class=\"error\">You must enter an <label for=\"email\">e-mail</label> address.</span><br />";
}
elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$", $email)) //if email entered, verify format
{
$error .= "<span class=\"error\">Your <label for=\"email\">email</label> is not of valid format.</span><br />";
}
if($semloc == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"semloc\">Seminar Location</label>.</span><br />";
}
if($semdate == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"semdate\">Seminar Date</label>.</span><br />";
}
if($semtime == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"semtime\">Seminar Time</label>.</span><br />";
}
if($agerange == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"agerange\">Please select your age range.</label>.</span><br />";
}
if($whyinterested == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"whyinterested\">Please select why your interested in the Brain Fitness Program.</label>.</span><br />";
}
if($howconvenient == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"howconvenient\">Please select how convenient this is for you.</label>.</span><br />";
}
if($idealtime == '')
{
$error .= "<span class=\"error\">You must enter your <label for=\"ideatime\">Please select an ideal time.</label>.</span><br />";
}
if($error == '') //If no errors
{
$sendTo = "Posit Science Seminar <rsvp@positscience.com>"; //this will be the address that the mail gets sent to
$subject = "RSVP Email";
$body = "You have received the following information from a RSVP submission:\r\n";
$body .= "---------------------------------------------------------------\r\n";
$body .= "Subject: Mail from Site\r\n";
$body .= "From: ".$email."\r\n";
$body .= "IP Address: ".$_SERVER['REMOTE_ADDR']."\r\n";
$body .= "FirstName: ".$firstname."\r\n";
$body .= "LastName: ".$lastname."\r\n";
$body .= "Address: ".$address."\r\n";
$body .= "City: ".$city."\r\n";
$body .= "State: ".$state."\r\n";
$body .= "ZipCode: ".$zipcode."\r\n";
$body .= "Phone: ".$phone."\r\n";
$body .= "Email: ".$email."\r\n";
$body .= "SemLoc: ".$semloc."\r\n";
$body .= "SemDate: ".$semdate."\r\n";
$body .= "SemTime: ".$semtime."\r\n";
$body .= "AgeRange: ".$agerange."\r\n";
$body .= "WhyInterested: ".$whyinterested."\r\n";
$body .= "HowConvenient: ".$howconvenient."\r\n";
$body .= "IdealTime: ".$idealtime."\r\n";
$body .= "BrainFitnessNews: ".$brainfitnessnews."\r\n";
$body .= "---------------------------------------------------------------\r\n";
$headers = 'From: '.$from."\r\n" .
'Reply-To: '.$email;
if(mail($sendTo, $subject, $body, $headers))
{
header("Location: /rsvp/thankyou.php");
}
else
{
$error = "<div class=\"error\"><span class=\"error\">Unfortunately, we were unable to send your e-mail. Please try again.</span></div>";
}
}
}
?>
Here is the drop down code:
<select name="agerange" id="agerange">
<option selected="selected">Please Select</option>
<option value="<?php echo $agerange; ?>">< 49</option>
<option value="<?php echo $agerange; ?>">50-59</option>
<option value="<?php echo $agerange; ?>">60-69</option>
<option value="<?php echo $agerange; ?>">70-79</option>
<option value="<?php echo $agerange; ?>">> 80</option>
</select>
Any ideas?
Thanks, Mac
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.