Log in

View Full Version : hard time with form validtion



genia
02-26-2009, 10:36 AM
hi guys,i'm really stuck:
i have a form which has few parts,for each part i do error check like this:

if (isset($_POST['flag'])) {
if (!ereg("^[a-zA-Z]+$", $firstName) || ($firstName=="")){
$e.="Please enter a valid Name<br>";
}
if (!ereg("^[a-zA-Z]+$", $lastName) || ($lastName=="")){
$e.="Please enter a valid Last name<br>";}
if (!(isValidEmail($email)) || ($email=="")){
$e.="Please enter a valid Email<br>";}
if ($countryOfResidence==""){$e.= "Please select Country of residence<br>";}
if ($phoneCountry==""){$e.= "Please select Phone country<br>";}
if (!is_numeric($areaCode) || $areaCode==""){$e.= "Please enter valid Area code<br>";}
if (!is_numeric($phoneNumber) || $phoneNumber==""){$e.= "Please enter valid Phone number<br>";}
if (!empty($e)){echo "<div class='error'>$e</div>";}
(the flag value comes from hidden filed in the form :
<input type='hidden' name='flag' value='1'>)

then after all of the six error checks i put this code:

$ws='0';
if (isset($_POST['flag'])) {
$ws='1';
if(($e==""&&$e2==""&&$e3==""&&$e4==""&&$e5==""&&$e6=="")&&($ws=='0')){}
if (($e==""&&$e2==""&&$e3==""&&$e4==""&&$e5==""&&$e6=="") && ($ws=='1')){echo "<div style='float:left;' class='clean-ok'>thank you2</div>";}}

and indeed if the form is blank and submitted nothing happens and if all errors are empty and the form is not blank the message "thank you2" displayed.
the problem is that i want to put this last code at the top and then transfer a redirect header to the browser,but if i do put the code at the top of the script it shows the "thank you2" at the top even if no submit was done.
any ideas how to solve this?
p.s. tried to echo to the html form hidden fileds like this:

if (!empty($e)){echo "<div class='error'>$e</div><input type='hidden' name='e1' value='1'>";}
and then check them,but it didnt work well too.

p.s. sorry for english mistakes-3rd languge[not counting php html css asp vb c]

JasonDFR
02-26-2009, 06:10 PM
I really didn't follow what you were saying about the redirect. You can redirect to a success page after your form has been submitted successfully, or you can use the same page.

I really don't understand this at all:


$ws='0';
if (isset($_POST['flag'])) {
$ws='1';
if(($e==""&&$e2==""&&$e3==""&&$e4==""&&$e5==""&&$e6=="")&&($ws=='0')){}
if (($e==""&&$e2==""&&$e3==""&&$e4==""&&$e5==""&&$e6=="") && ($ws=='1')){echo "<div style='float:left;' class='clean-ok'>thank you2</div>";}}


I made some changes to your code. Let me know what you think.



<?php

if (isset($_POST['flag'])) {

$e = array(); // An empty array to hold possible error messages

if (!ereg("^[a-zA-Z]+$", $firstName) ) {
$e[] = 'Please enter a valid Name<br>';
}

if (!ereg("^[a-zA-Z]+$", $lastName) ) {
$e[] = 'Please enter a valid Last name<br>';
}

if ( !(isValidEmail($email)) ) {
$e[] = 'Please enter a valid Email<br>';
}

if ( $countryOfResidence=='' ) {
$e[] = 'Please select Country of residence<br>';
}

if ( $phoneCountry=='' ) {
$e[] = 'Please select Phone country<br>';
}

if ( !is_numeric($areaCode) ) {
$e[] = 'Please enter valid Area code<br>';
}

if ( !is_numeric($phoneNumber) ) {
$e[] = 'Please enter valid Phone number<br>';
}

if ( empty($e) ) { // No errors occurred

// Process your form here

if ( your form processing was successful ) {

$success = true; // OR send to another page: header('Location: /thankyou.php');

}

}

}
?>

<html>

<head>
<title></title>
</head>

<body>

<?php

if ( isset($success) ) { // If you are not redirecting to a success page.

echo '<p>Thank you.</p>';

} else { // Form either hasn't been submitted or wasn't successful

if ( !empty($e) ) { // If errors exist, echo them.

foreach ( $e as $msg ) {

echo $msg;

}

}
?>

<!-- YOUR FORM HERE action="<?php echo $_SERVER['PHP_SELF']; ?>" -->

<?php
}
?>

</body>

</html>


I think it is a bit cleaner now and should work how you'd like. It's not ready to use as is, you'll have to process the form, and insert the HTML for the form. The validation should be good to go though. Let me know if you have any questions.

Good luck,

J

genia
02-26-2009, 06:50 PM
wow,thank you very much for taking the time and going over my code.
this is nice,but it's helpful only if i want to display the errors on the top of the form.
anyway problem solved by putting all the checks at the top and only echoing the error in the mid,that way i can check if there are no errors and redirect etc.

thank you again