Forum Moderators: coopster

Message Too Old, No Replies

Figuring out form validation

         

broC

7:38 am on Feb 19, 2008 (gmt 0)

10+ Year Member



had problem with my coding..its prompt error if i leave the form blank then submit,however,its still query and go to databsee..can sumbody check for me n teach me wheres mymistake



<table width="1000" height="95" border="1">
<tr>
<td bgcolor="#0000FF">&nbsp;</td>
</tr>
</table>

<center>

<?
include("connect.php");
//this is your validation in the form,put it here....
{ if (empty($_POST['name']))
{
$errors[] = 'Please enter a name';
}
if (empty($_POST['email']))
{
$errors[] = 'Please enter a valid e-mail address';
}
else if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$",
$_POST['email']))
{
$errors[] = 'Please enter a valid e-mail address';
}
if (empty($_POST['contact']))
{
$errors[] = 'Please enter a valid contact with numeric value';
}
else if (!is_numeric($_POST['contact']))
{
$errors[] = 'Please enter a valid contact with a numeric value';
}


if (empty($_POST['person_attend']))
{
$errors[] = 'Please enter some word for person attend';
}


else if (strlen ($_POST['person_attend']) > 255)
{
$errors[] = 'person attend';
}



if (empty($_POST['comment']))
{
$errors[] = 'Please enter some comment';
}
else if (strlen ($_POST['comment']) > 255)
{
$errors[] = 'comment ';

}

if(isset($errors))
{

foreach($errors as $val)
{

echo "Error: $val <br/>";
}
}


if($name="" && $email="" && $contact="" && $person_attend="" && $comment="" )

{

//insert statements

} else {

//error message

}

//process form

//this is your add query....

$name = $_POST['name'];

$email = $_POST['email'];

$contact = $_POST['contact'];

$person_attend = $_POST['person_attend'];

$comment = $_POST['comment'];

$query = "INSERT INTO rsvp (id, name, email, contact, person_attend, comment)

VALUES ('', '$name', '$email', '$contact', '$person_attend', '$comment')";

$results = mysql_query($query) or die

("Could not execute query : $query." . mysql_error());

{

echo "thanks you ";

}

mysql_close();
}

?>
</center>
<center>
<br>
<a href="Index.php">View list attend</a><br>
<a href="register.html">Clik here to register</a>
</center>

deMorte

11:24 am on Feb 19, 2008 (gmt 0)

10+ Year Member



There seems to be few issues in your code. Here's what I spotted.

You should create the variables you are testing before you use them. So you have to move this


$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$person_attend = $_POST['person_attend'];
$comment = $_POST['comment'];

above testing. Also, you should note that the comparison operator is '==', not '='.

if($name=="" && $email=="" && $contact=="" && $person_attend=="" && $comment=="" ) {

Also, you stating that if all the inputs are empty there is an insert? This is probably not what you want?

This is how I would implement your code:


$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$person_attend = $_POST['person_attend'];
$comment = $_POST['comment'];

if (empty($name))
$errors[] = 'Please enter a name';
}

// other error testings in the same way...

if(!is_array($errors)) {

$query = "INSERT INTO rsvp (id, name, email, contact, person_attend, comment) VALUES ('', '$name', '$email', '$contact', '$person_attend', '$comment')";

$results = mysql_query($query) or die
("Could not execute query : $query." . mysql_error());

mysql_close();

} else {

foreach($errors as $val) {
echo "Error: $val <br/>";
}
}


So, if $errors is not an array (there are no errors) you go to Insert-code, otherwise you print out the errors.

Hope this helps.

PS. I did not test this code so there could be typos in there.