Simple validation
Posted: Mon Oct 06, 2014 12:44 am
Hi guys, this is my very first topic so bear with me
I'm still learning how to validate but I really need your help.
I need to validate first name, last name, street, suburb, postcode, email, status and date of birth.
If I do this way:
It gave me heaps of error and it won't work good.
This is my full PHP code:
I don't know what do to and I'm really confused so I was wondering if you could help me out?
Your help would be greatly appreciated!
Thank you.
I'm still learning how to validate but I really need your help.
I need to validate first name, last name, street, suburb, postcode, email, status and date of birth.
If I do this way:
Code: Select all
if (empty($firstname)) {$errors[] =" First Name Can not be Empty <br> ";}
if (empty($lastname)) {$errors[] =" Last Name Can not be Empty <br> ";}
if (empty($street)) {$errors[] =" Street Can not be Empty <br> ";}
if (empty($suburb)) {$errors[] =" Suburb Can not be Empty <br> ";}
if (empty($postcode)) {$errors[] =" Postcode Can not be Empty <br> ";}
// elseif (!is_numeric($postcode)) {$errors[] =" Postcode must be numeric ";}
elseif(!preg_match("/\^\(\[0\-9\]\{5\}\(\[\-\\s\]\?\[0\-9\]\{4\}\)\?\)\$/", $postcode)) {$errors[] =" Please enter a valid post number <br> ";}
if( !preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i", $myemail) ) {$errors[] =" You have entered and invalid email address <br> ";}
if (empty($DOB)) {$errors[] =" Date only <br> ";} This is my full PHP code:
Code: Select all
<html>
<head>
<style>
body
{
background-color:#d0e4fe;
}
h1
{
color:orange;
text-align:center;
}
p
{
font-family:"Times New Roman";
font-size:20px;
}
table,th,td
{
border:1px solid black;
}
table
{
width:90%;
}
th
{
height:40px;
}
</style>
</head>
<body>
<h1>All Students Information</h1>
</body>
</html>
<?php
function renderForm($first, $last, $st, $sub, $pcode, $em, $sta, $dofb, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Add Student</title>
</head>
<body>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
echo "<p><b>Add new student details</b></p>";
?>
<form action="" method="post">
<div>
<strong>First name: </strong> <input type="text" name="FNAME" value="<?php echo $first; ?>" /><br/>
<strong>Last name: </strong> <input type="text" name="LNAME" value="<?php echo $last; ?>" /><br/>
<strong>Street: </strong> <input type="text" name="STREET" value="<?php echo $st; ?>" /><br/>
<strong>Suburb: </strong> <input type="text" name="SUBURB" value="<?php echo $sub; ?>" /><br/>
<strong>Postcode: </strong> <input type="text" name="POSTCODE" value="<?php echo $pcode; ?>" /><br/>
<strong>Email: </strong> <input type="text" name="EMAIL" value="<?php echo $em; ?>" /><br/>
<strong>Status: </strong> <input type="text" name="STATUS" value="<?php echo $sta; ?>" /><br/>
<strong>Date of Birth: </strong> <input type="text" name="DOB" value="<?php echo $dofb; ?>" /><br/>
<p>Required Field</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
<p><a href="view.php">Update & delete students</a></p>
</body>
</html>
<?php
}
include('dbconnect.php');
if (isset($_POST['submit']))
{
$firstname = mysql_real_escape_string(htmlspecialchars($_POST['FNAME']));
$lastname = mysql_real_escape_string(htmlspecialchars($_POST['LNAME']));
$street = mysql_real_escape_string(htmlspecialchars($_POST['STREET']));
$suburb = mysql_real_escape_string(htmlspecialchars($_POST['SUBURB']));
$postcode = mysql_real_escape_string(htmlspecialchars($_POST['POSTCODE']));
$email = mysql_real_escape_string(htmlspecialchars($_POST['EMAIL']));
$status = mysql_real_escape_string(htmlspecialchars($_POST['STATUS']));
$dob = mysql_real_escape_string(htmlspecialchars($_POST['DOB']));
if ($firstname == '' || $lastname == '' || $street == '' || $suburb == '' || $postcode == '' || $email == '' || $status == '' || $dob == '')
{
$error = 'Your field is empty';
renderForm($firstname, $lastname, $street, $suburb, $postcode, $email, $status, $dob, $error);
}
else
{
mysql_query("INSERT student SET FNAME='$firstname', LNAME='$lastname', STREET='$street', SUBURB='$suburb', POSTCODE='$postcode', EMAIL='$email', STATUS='$status', DOB='$dob' ")
or die(mysql_error());
header("Location: view.php");
}
}
else
{
renderForm('','','','','','','','','');
}
?>Your help would be greatly appreciated!
Thank you.