View Full Version : Form Field Error Page
imagewise
05-06-2008, 07:08 PM
I want to add script that tells the user they forgot a 'required' field... anyone???
This is how I do it for a simple Inquiry form. Put this in the <head> section:
<script language=Javascript type="text/javascript">
function validate() {
if (document.infoform.name.value == "") {
alert("Please fill in your name.");
return false;
}
if (document.infoform.email.value == "") {
alert("Please fill in your email address.");
return false;
}
return true;
}
</script>
Then in the form definition I put this:
<form name="infoform" method="POST" action="/php/infoform.php" onsubmit="return validate()" >
Hope this helps you. erin :)
AfroJoJo
05-17-2008, 07:11 AM
Its best to have a combination of javascript and a server side script for form validation. A server side script is more important though.
<?php
if (!isset($_POST['submit'])) {
showForm();
} else {
$error = 0;
if(empty($_POST['name'])) {
$error = 1;
$errstr[] = "Please enter your name";
}
if(!preg_match("/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/", $_POST['email'])) {
$error = 1;
$errstr[] = "Please enter a valid email address";
}
if(empty($_POST['subject'])) {
$error = 1;
$errstr[] = "Please enter a subject";
}
if(empty($_POST['message']) || preg_match("/^enter your message here$/i", $_POST['message'])) {
$error = 1;
$errstr[] = "Please enter a message";
}
if ($error == 1) {
echo "<span style='color:#FF0000;'>";
foreach($errstr as $err) {
echo $err . "<BR>";
}
echo "</span><BR>";
showForm();
} else {
@mail("email@example.com", $_POST['subject'] , stripslashes($_POST['message']) , "From: " . $_POST['name'] . "<" . $_POST['email'] . ">" , "-f" . $_POST['email']);
echo "<span style='color:#000000; font-size:30px;'>Thank You</span><BR><span style='color:#FF0000; font-size:25px;'>Submitted Successfully</span>";
}
}
function showForm()
{
$_POST['message'] = @htmlspecialchars(@$_POST['message']);
echo <<<EOD
<form method="POST">
<b>Name:</b> <span style='color:#FF0000; font-weight: bold;'>*</span>
<input type"text" name="name" value="{$_POST['name']}">
<BR><BR>
<b>Email:</b> <span style='color:#FF0000; font-weight: bold;'>*</span>
<input type="text" name="email" value="{$_POST['email']}">
<BR><BR>
<b>Subject:</b> <span style='color:#FF0000; font-weight: bold;'>*</span>
<input type="text" name="subject" value="{$_POST['subject']}">
<BR><BR>
<b>Message:</b> <span style='color:#FF0000; font-weight: bold;'>*</span><BR>
<textarea name="message" rows="8" cols="40">{$_POST['message']}</textarea><BR><BR>
<input style="width: 125px;" type="submit" name="submit" value="Send">
</form>
EOD;
}
?>
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.