Page 1 of 1

Form Validation Alert

Posted: Tue Nov 25, 2008 3:01 pm
by packFan12
So i've got a form that has a few text fields and some check boxes on it & on my php page I have a validation, as shown below, so that if one of the fields or checkbox is empty/unselected it goes back to the form. What I want to add is an alert box that shows up after it goes back to the page so the visitor knows the form wasn't actually submitted due to the form not being completed.

Here's the code from the php page:

Code: Select all

 
<?
if (($_POST[senderName] == "") ||
    ($_POST[senderEmail] == "") ||
    ($_POST[message] == "") ||
    (!isset($_POST['check']))) {
     header("Location: contact.htm");
     exit;
}
 
$to = "me@site.com";
$subject = "CONTACT INQUIRY";
$mailheaders = "From: Info <me@site.com>\n";
$mailheaders .= "Reply-To: $_POST[senderEmail]\n";
 
foreach($_POST['check'] as $value) {
    $checkBox .= "$value\n";
}
 
$msg .= "Senders Name:\t$_POST[senderName]\n";
$msg .= "Senders Email:\t$_POST[senderEmail]\n";
$msg .= "Message:\t$_POST[message]\n";
$msg .= "Checked boxes:\t $checkBox\n";
 
mail($to, $subject, $msg, $mailheaders);
?>
 
I know an easy fix to this would be to make the header("Location: contact.htm") go to a secondary contact page, but I'd prefer to have a popup but am not sure how to do this. I've also tried adding a few things above and below header("Location: contact.htm") that have been unsuccessful, so if anyone has any ideas it would be much appreciated!

Re: Form Validation Alert

Posted: Tue Nov 25, 2008 3:22 pm
by sparrrow
If you are going to go as far as to do all that, then maybe you should validate the fields BEFORE they leave the page, using Javascript. Add this in your <head> section

Code: Select all

<script language="javascript" type="text/javascript">
 
<!-- hide script from older browsers
function validateForm(formname)
{
var good=true;
if(document.forms.formname.fieldname.value=="")
{
alert("Please enter your [fieldname].");
document.forms.formname.fieldname.focus();
good=false;
return false;
}
 
if (good) {
alert('Form has been validated. Thank you for your submission.');
}
}
stop hiding script -->
</script>
Then in your <form> tag, add an onSubmit action:

Code: Select all

<form action="page.php" method="post" name="formname" onSubmit="return validateForm(formname);"> 
 
Of course, substitute formname and fieldname for your real object names. Also feel free to copy paste lines 7-13 in the JS to check more fields (use actual field names and messages).

Re: Form Validation Alert

Posted: Tue Nov 25, 2008 4:04 pm
by Stryks
Using javascript for client-side validation is a good idea. It saves you the page load and makes for a much better user experience if done right.

But don't fall into the trap of thinking that it is enough to do just that. Optimally, your site should work as intended regardless of the presence of javascript. With that in mind, you'll soon realize that this means two, almost identical, layers of validation. One server side (PHP) and another client side (javascript).

But in direct answer to your question, I wouldn't use a popup at all with the server side content. Instead, I'd advise that you embed an error message in the page itself. Sure it wont get as much attention, but the user wont be tripping over it, and more importantly, it wont just silently fail if javascript is not present / enabled.

Then, if javascript is enabled, you click submit and you get your failure popup. If there is no javascript, or the form fails validation on the PHP end, you still get an error message and a re-display of the form.

Either way, the PHP-side validation is the one you will always fall back on, so it's the one that needs the most attention.

Cheers

Re: Form Validation Alert

Posted: Wed Nov 26, 2008 2:23 pm
by packFan12
thanks you guys, neither of those ideas crossed my mind :) i'll give that a try over the holiday weekend!