PDA

View Full Version : Preventing Blank Inquiries



kuau
12-27-2007, 06:43 PM
I am using the following little script to make sure people fill in the required fields in an inquiry form:

<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>

It seems to work when I test it; nevertheless, a lot of blank inquiries still get through. I am wondering if this could be bots somehow messing with it, or people who have javascript turned off. Is there a better way to prevent blank entries? I don't want to discourage people from making inquiries by making them jump through too many hoops.

Thanks for your help. erin :)

thetestingsite
12-27-2007, 07:14 PM
Javascript is not very reliable for this, and it is recommended that you use a server side fallback to check for blank fields that should be required.

Hope this helps.

kuau
12-27-2007, 07:26 PM
What's a server side fallback?

thetestingsite
12-27-2007, 07:31 PM
How is your form being submitted? Is it through a php script (like form.php), or some other form of server side scripting. If so, you would want to put a snippet of code in that file to check to see if there are blank submissions or whatnot.

Hope this helps.

kuau
12-27-2007, 07:47 PM
Yes, a php script, but it comes into play only after the form is submitted. I don't know how to do a pop-up window in php so would have to send them to an error page and by then the person would have gone away. Is there an easy way to do the php check? If not, can't I just improve that javascript? What do other people do? Thanks, erin

boogyman
12-27-2007, 08:10 PM
Yes, a php script, but it comes into play only after the form is submitted. I don't know how to do a pop-up window in php so would have to send them to an error page and by then the person would have gone away. Is there an easy way to do the php check? If not, can't I just improve that javascript? What do other people do? Thanks, erin

submit it to a validation function and in the validation function you check for each required field. if that field contains an error you place the a message containing the field into an error array.

at the very end of the validation function after you have done all of your checks on individual fields you do one last check to see if the error array has been populated. if there are values in the error array you send the user back to the form notifying them of the errors (typically its done in a list in red / outstanding color)

This creates some extra work but you shouldn't force the user to re-enter every single value again, but rather save each of the form fields and populate all of the ones that are correct with the previous values to prevent duplication, and call attention to the fields that are in error (typically with a different background color)

That last bit about auto-populating the old values will create less frustration for the user, but it's only to help keep the user from being frustrated and leaving your page...

that may have been hard to understand so here is some psuedo code


<?php

function process_form()
{
$error = array();
if(field required)
{
condition
if( bad_condition )
{
$error[] = "Message about required field";
}
}
...

if(isset($error))
{
display_form($errors);
}
}

function display_form($error)
{

if($error)
{
echo "<ul>";
foreach($error as $err)
{
echo "\n\t<li>". $err ."</li>";
}
echo "</ul>";
?>
--- REST OF FORM INFORMATION HERE ---
<?php
}
}


if(submitted)
{
process_form();
}
else
{
display_form();
}

kuau
12-28-2007, 12:46 AM
Dear Boogey:

Wow, that was a bit over my head, but when I get some time I'll see if I can figure out what it is. I know only the tiniest bit of php... I'm just a good mimmick. ie. if I have some code that is correct as a model, I can usually apply it to another situation but, out of context, it might as well be Japanese. Am I supposed to insert that into the html code? I can't really focus on it right now (working) but shall try later. Thanks for your help. Aloha, erin :)

thetestingsite
12-28-2007, 01:00 AM
If you can post the code that you have now that processes the form, we can take a look at it and modify it to have the correct field checking snippet.

Hope this helps.

kuau
12-28-2007, 02:33 AM
What a sweetheart you are. I have to admit not knowing how to post code so neatly as Boogy did. It made a mess when I tried to put it here. So maybe you can steer me to that tutorial, and also clarify if it is the form code in the web-facing page or the php code that runs when you hit Submit that you want to see. Mahalo plenty, erin :)

boogyman
01-03-2008, 08:00 PM
you can post code like I did by using




your_stuff




clarify if it is the form code in the web-facing page or the php code that runs when you hit Submit that you want to see

both would be great.

kuau
01-03-2008, 09:17 PM
Thanks