Click to See Complete Forum and Search --> : [RESOLVED] Form PHP doesn't return false if a required field is empty


kschroers
07-23-2008, 01:10 PM
I'm not too familiar with forms and php so I grabbed a template from an online source. My form works as in it submits the info to the correct email but my required fields are supposed to give an error if they are not filled in or incorrect and they aren't doing that. I'm not sure how to fix this because it is a foreign language to me. Here is the url I need help with. Please help me!

http://creativcat.com/projects/rapidweb/catalog.html

Here is the php I am using:

<?php

$to="email";

$header="From: ".$_POST['email'] . "\r\n";
$header .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$subject="Rapid Labels Catalog Order Form";

$body='<html><body>
'.$_POST['fname'].'
'.$_POST['lname'].' <br>
'.$_POST['email'].' <br>
'.$_POST['address'].' <br>
'.$_POST['city'].' <br>
'.$_POST['state'].' <br>
'.$_POST['zip'].' <br>
'.$_POST['phone'].' <br><br>
'.$_POST['comments'].'
</body></html>';

mail($to,$subject,$body,$header);

?>

Phill Pafford
07-23-2008, 01:25 PM
foreach $_POST element you shold validate if it is empty and conforms to what you want


// Could use for first and last name
function validate_name($name)
{
if(empty($name))
{
return false;
}
else if(preg_match("^/[a-zA-Z/+$", $name)
{
return true;
}
else
{
return false;
}
}

if(!validate_name($_POST['fname'])
{
echo "Please fill in your first name<br />";
}


haven't testing this but something to get you started

kschroers
07-23-2008, 01:46 PM
Thanks Phill but I am still confused. This is what I was told to add to the script to get the fields to return false so maybe this is what I need to be fixing.

function isEmpty(elem, helperMsg)
{
if(elem.value.length == 0)
{
alert(helperMsg);
elem.focus();
return false;
}
else
{return true;}
}

function validate_email(field,alerttxt)
{
with (field)
{
atposition=value.indexOf("@")
dotposition=value.lastIndexOf(".")

if (atposition<1||dotposition-atposition<2)
{alert(alerttxt);return false}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (isEmpty(document.getElementById('fname'), 'Please enter a first
name')==false)
{fname.focus();return false}

if (isEmpty(document.getElementById('lname'), 'Please enter a last
name')==false)
{lname.focus();return false}

if (isEmpty(document.getElementById('email'), 'Please enter an email
address')==false)
{email.focus();return false}

if (isEmpty(document.getElementById('address'), 'Please enter a street
address')==false)
{address.focus();return false}

if (isEmpty(document.getElementById('city'), 'Please enter a city
')==false)
{city.focus();return false}

if (isEmpty(document.getElementById('state'), 'Please enter a state
')==false)
{state.focus();return false}

if (isEmpty(document.getElementById('zip'), 'Please enter a zip code
')==false)
{zip.focus();return false}

if (isEmpty(document.getElementById('comments'), 'Please enter a
comment or question')==false)
{comments.focus();return false}

if (validate_email(email,"Please enter a valid email
address")==false)
{email.focus();return false}
}
}

Phill Pafford
07-23-2008, 02:01 PM
this looks like JavaScript


function isEmpty(elem, helperMsg)
{
if(elem.value.length == 0)
{
alert(helperMsg);
elem.focus();
return false;
}
else
{return true;}
}

function validate_email(field,alerttxt)
{
with (field)
{
atposition=value.indexOf("@")
dotposition=value.lastIndexOf(".")

if (atposition<1||dotposition-atposition<2)
{alert(alerttxt);return false}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (isEmpty(document.getElementById('fname'), 'Please enter a first
name')==false)
{fname.focus();return false}

if (isEmpty(document.getElementById('lname'), 'Please enter a last
name')==false)
{lname.focus();return false}

if (isEmpty(document.getElementById('email'), 'Please enter an email
address')==false)
{email.focus();return false}

if (isEmpty(document.getElementById('address'), 'Please enter a street
address')==false)
{address.focus();return false}

if (isEmpty(document.getElementById('city'), 'Please enter a city
')==false)
{city.focus();return false}

if (isEmpty(document.getElementById('state'), 'Please enter a state
')==false)
{state.focus();return false}

if (isEmpty(document.getElementById('zip'), 'Please enter a zip code
')==false)
{zip.focus();return false}

if (isEmpty(document.getElementById('comments'), 'Please enter a
comment or question')==false)
{comments.focus();return false}

if (validate_email(email,"Please enter a valid email
address")==false)
{email.focus();return false}
}
}


I like PHP server side validation instead. if you want JavaScript help you should post in their forum.

Also when do you call the JavaScript validation, on submit? in your PHP code I only see a mail function.

kschroers
07-23-2008, 02:55 PM
I have no idea.. like I said I just copied this off an online source. I think I got in over my head with all this php and javascript talk. Thanks for you help though.:o

If you have a better reference, let me know.

Phill Pafford
07-23-2008, 03:03 PM
Ok it looks like what you are trying to do is add some code to validate your input.

On the page with the form there should be a submit button. in the code where the submit button is there should be a onclick method, something like onclick="validate_form(this.form)"

if not add it and see what it does