Welcome to WebmasterWorld Guest from 3.84.130.252

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

displaying multiple validation errors instead of 1 error at a time

     
4:58 am on Aug 31, 2008 (gmt 0)

New User

10+ Year Member

joined:Aug 24, 2008
posts: 5
votes: 0


hi,
i am trying to modify my php server side script code to display all errors upon validation check instead of displaying only 1 error at a time.

Presently, my code finds the first error on validation and then displays it. Once the error been corrected by the user, the validation routine displays the next error upon resubmission and displays that error. This routine is very time consuming and i would like to see all errors displayed at once for the user to see and correct.
your help would be very appreciative.
thanks
larrym

here is the php code of the form page:

<?php
session_start();
if(isset($_SESSION['error']))
echo $_SESSION['error'];
?>

here is the session part of the server side script:

session_start();

$_SESSION['from'] = $from;
if (strlen($from) <2)
{header("Location: contact-us.php?status=0");
$_SESSION['error'] = 'Please enter your name';
exit;}

$_SESSION['email'] = $email;
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{header("Location: contact-us.php?status=0");
$_SESSION['error'] = 'Please enter your your correct email';
exit;}

$_SESSION['telnr'] = $telnr;
if( (strlen($telnr)<7) Or (strlen($telnr)>14) )
{header("Location: contact-us.php?status=0");
$_SESSION['error'] = 'Please enter your correct phone number';
exit;}

$_SESSION['street'] = $street;
if (strlen($street) <3)
{header("Location: contact-us.php?status=0");
$_SESSION['error'] = 'Please enter your street';
exit;}

$_SESSION['city'] = $city;
if (strlen($city) <2)
{header("Location: contact-us.php?status=0");
$_SESSION['error'] = 'Please enter a valid city';
exit;}

$_SESSION['state'] = $state;
if (strlen($state) == 0 )
{header("Location: contact-us.php?status=0");
$_SESSION['error'] = 'Please choose a valid state';
exit;}

$_SESSION['zipcode'] = $zipcode;
if( (strlen($zipcode)<5) Or (strlen($zipcode)>10) )
{header("Location: contact-us.php?status=0");
$_SESSION['error'] = 'Please enter a valid zipcode';
exit;}

$_SESSION['subject'] = $subject;
if (strlen($subject) == 0 )
{header("Location: contact-us.php?status=0");
$_SESSION['error'] = 'Please choose a valid subject';
exit;}

if ($_POST['captcha'] != $_SESSION['captchacode'])
{header("Location: contact-us.php?status=2");
$_SESSION['error'] = 'You did not enter the letters shown in the image for validation';
captchaDone();
exit;}

8:33 am on Aug 31, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 0


This can easily be done by just adding each error to an error string, then sending the string once all validation is done.

$error = '';

if (strlen($from) <2)
{
$error .= 'Please enter your name<br />';
}
if( (strlen($telnr)<7) Or (strlen($telnr)>14) )
{
$error .= 'Please enter your correct phone number<br />';
}
// etc.

if($error)
{
$_SESSION['error'] = $error;
header("Location: contact-us.php?status=0");
die;
}

1:11 pm on Aug 31, 2008 (gmt 0)

New User

10+ Year Member

joined:Aug 24, 2008
posts: 5
votes: 0


thank you for your quick response
it worked perfectly
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members