Hi,
Ive created a form and validated it using php. It validates ok but if a field is left blank, the prompt message appears, which then clears all entries made in the form. Im not sure how to fix this. any suggestions?
Thanks
heres the code:
<?php
if(!isset($POST['submit']))
{
if(isset($GET['pass1']))
{
echo "Password Field Cannot Be Left Blank";
}
if(isset($GET['pass2']))
{
echo "Confirm Password Field Cannot Be Left Blank";
}
if(isset($GET['pass3']))
{
echo "Passwords do not match";
}
if(isset($GET['address']))
{
echo "Address Field Cannot Be Left Blank";
}
if(isset($GET['firstname']))
{
echo "First Name Field Cannot Be Left Blank";
}
if(isset($GET['lastname']))
{
echo "Last Name Field Cannot Be Left Blank";
}
if(isset($GET['email1']))
{
echo "Email Field Cannot Be Left Blank";
}
if(isset($GET['email2']))
{
echo "Not a valid email address";
}
if(isset($GET['phone']))
{
echo "Phone Field Cannot Be Left Blank";
}
?>
<html>
<head>
<title>Insert Test</title>
</head>
<body>
<h1>Insert Test</h1>
<form action = "<?$_SERVER['PHP_SELF']?>" method = "post">
<ul><li>First Name<input type = "text" size = "20"name= "first_name" /> </li>
<li>Last Name<input type = "text" size = "20" name = "last_name" /> </li>
<li>Password<input type = "password" size = "20" name = "password" /> </li>
<li>Confirm Password<input type = "password" size = "20" name = "confirm_password" /> </li>
<li>Email<input type = "text" size = "20" name = "email" /> </li>
<li>Address <textarea name = "address" rows = "5" colums = "20" /></textarea> </li>
<li>Area <select size = "1" name = "area">
<option value = "Carlow">Carlow</option>
<option value = "Cavan">Cavan</option>
</select> </li>
<li>Phone No<input type = "text" size = "20" name = "phone_no" /> </li>
</ul>
<input type = "submit" name = "submit" value = "Click Here">
</form>
</body>
</html>
<?php
}
else
{
$db = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("hunty", $db);
$status = true;
$FirstName = $_POST['first_name'];
$LastName = $_POST['last_name'];
$Password = $_POST['password'];
$Password_confirm = $_POST['confirm_password'];
$Email = $_POST['email'];
$Addr = $_POST['address'];
$area = $_POST['area'];
$PhoneNo= $_POST['phone_no'];
if($PhoneNo == "")
{
$status = false;
$url = "Location: regtest.php?phone";
header($url);
}
if($Addr == "")
{
$status = false;
$url = "Location: regtest.php?address";
header($url);
}
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email))
{
$status = false;
$url = "Location: regtest.php?email2";
header($url);
}
if($Email == "")
{
$status = false;
$url = "Location: regtest.php?email1";
header($url);
}
if($Password != $Password_confirm)
{
$status = false;
$url = "Location: regtest.php?pass3";
header($url);
}
if($Password_confirm == "")
{
$status = false;
$url = "Location: regtest.php?pass2";
header($url);
}
if($Passowrd = '')
{
$status = false;
$url = "Location: regtest.php?pass1";
header($url);
}
if($LastName == '')
{
$status = false;
$url = "Location: regtest.php?lastname";
header($url);
}
if($FirstName=='')
{
$status = false;
$url = "Location: regtest.php?firstname";
header($url);
}
if($status == true)
{
$query = mysql_query("insert into buyer(first_name, last_name, password, email, addr, area, phone)values('$FirstName','$LastName','$Password','$Email','$Addr','$area', '$PhoneNo')");
echo "Data has been succesfully ";
}
}
?>