Web Hosting Talk







View Full Version : php form validation problem


NorthWest
12-08-2005, 12:29 PM
Hey Everyone, I am creating an email form and I have a question about the form validation. The validation is working perfectly, but I was wondering what I have to do to make it print the message on the page that has the form fields instead of having it print the message on a separate blank page. I am guessing because I am stopping the script with the exit() / die so it cant read the html below, but I don’t know how to go about making it continue to the html while also printing the message that a form wasn’t completed. Thanks in advance.

Here is my PHP:

if ($_POST['processform']==1)
{
$name = $_POST['name'];
$email = $_POST['email'];
$description = $_POST['description'];
$today = date('Y-m-d');
$to = 'myemail@email.com';
$subject = 'subject';
$str .= "
Name: $name
Email: $email
Date Sent: $today
Description: $description
";
$headers .= "X-Priority: 2\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";

if (!$name) {
print ("Please fill in the name field");
exit();
}

if (!$email) {
print ("Please fill in the email field");
exit();
}

if (!$description) {
print ("Please fill in the description field");
exit();
}



mail($to, $subject, $str, $headers);
}








And Here is my form




<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="email">

<input type="hidden" name="processform" value="1">

<tr>
<td width="100" class="formlabel">Name:</td>
<td class="field"><input name="name" type="text" size="30" ></td>
</tr>
<tr>
<td width="100" class="formlabel">Email:</td>
<td class="field"><input name="email" type="text" size="30"></td>
</tr>
<tr>
<td width="100" valign="top" class="formlabel">Description:</td>
<td class="field"><textarea cols="30" rows="10" name="description"></textarea></td>

Korvan
12-08-2005, 12:38 PM
try:

$error = NULL;
if ($_POST['processform']==1)
{
$name = $_POST['name'];
$email = $_POST['email'];
$description = $_POST['description'];
$today = date('Y-m-d');
$to = 'myemail@email.com';
$subject = 'subject';
$str .= "
Name: $name
Email: $email
Date Sent: $today
Description: $description
";
$headers .= "X-Priority: 2\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";

if (!$name) {
$error = "Please fill in the name field";

}

if (!$email) {
$error = "Please fill in the email field";

}

if (!$description) {
$error = "Please fill in the description field";

}


if(!$error)
mail($to, $subject, $str, $headers);
}








And Here is my form




<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="email">

<input type="hidden" name="processform" value="1">
<?php
if($error)
{
?>
<tr><td colspan="2"><?php echo $error;?></td></tr>
<?php
}
?>
<tr>
<td width="100" class="formlabel">Name:</td>
<td class="field"><input name="name" type="text" size="30" value="<?php echo $name;?>"></td>
</tr>
<tr>
<td width="100" class="formlabel">Email:</td>
<td class="field"><input name="email" type="text" size="30" value="<?php echo $email;?>"></td>
</tr>
<tr>
<td width="100" valign="top" class="formlabel">Description:</td>
<td class="field"><textarea cols="30" rows="10" name="description"><?php echo $description;?></textarea></td>

NorthWest
12-08-2005, 01:16 PM
Thanks Korvan. It worked.

AdvaHost
12-08-2005, 03:11 PM
Also you may want to validate use regular expressions. Such as:

//Email Validation
if(eregi ("^([[:alnum:]]|_|\.|-)+@([[:alnum:]]|\.|-)+(\.)+([a-z]{2,4})$", $_POST['email'])){
$return = true;
}else{
$return = false;
$message[] = "Please enter a valid email address";
}

You may want to implement form security by using: strip_tags(), mysql_real_escape_string(), or addslashes().