I am still a newbie with PHP and learning.
Working on a sticky form that is supposed to validate for empty field and report errors if it is empty. It also has to be redirected to the payment page if no errors found.
I have to use sessions though the form doesn't seem to be working. Can anybody help me with this?
Thankssession_start();
// Set errors variable to empty
$errors = "";
if(isset($_POST['submit'])) {
//Use is_numeric to ensure the value is numeric, trim and strlen to ensure the field is not empty, validate the email field with a regular expression,
if (!is_numeric($_POST['tel'])) {
$errors = "Telephone field cannot be empty and must be numeric";
echo ("<font color ='#ff0000'>$errors</font><br>");
}
// Name must have a space between first name and last name - use regular expression ??
if (strlen($_POST['name']) == 0){
$errors = "Full Name is required and must contain a space between the first name and last name";
echo ("<font color ='#ff0000'>$errors</font><br>");
}
if (strlen($_POST['address']) == 0){
$errors = "Address is required";
echo ("<font color ='#ff0000'>$errors </font><br>");
}
if (strlen($_POST['city']) == 0){
$errors = "City is required";
echo ("<font color ='#ff0000'>$errors </font><br>");
}
if (strlen($_POST['provstate']) == 0){
$errors = "Province or State is required";
echo ("<font color ='#ff0000'>$errors </font><br>");
}
if (strlen($_POST['country']) == 0){
$errors = "Select Country from the list";
echo ("<font color ='#ff0000'>$errors </font><br>");
}
if (strlen($_POST['email']) == 0) {
$errors = "Email address cannot be empty and must be in this format";
echo ("<font color ='#ff0000'>$errors </font><br>");
}
$_SESSION['selected_name'] = $_POST['name'];
$_SESSION['selected_company'] = $_POST['company'];
$_SESSION['selected_address'] = $_POST['address'];
$_SESSION['selected_city'] = $_POST['city'];
$_SESSION['selected_provstate'] = $_POST['provstate'];
$_SESSION['selected_country'] = $_POST['country'];
$_SESSION['selected_tel'] = $_POST['tel'];
$_SESSION['selected_email'] = $_POST['email'];
}
?>
<html>
<head>
<h2>Billing Information</h2>
</head>
<body>
<font face = "Verdana" size="2">
<form name="billing" method = "POST" action = "billing.php">
<br>
<h3>Cardholder Details</h3>
<p>Name: <input type = "text" size="50" maxlength = "50" name="name" value ="<?php echo $_POST['name'];?>"></p>
<p>Company: <input type = "text" size="50" maxlength = "100" name="company" value ="<?php echo $_POST['company'];?>"></p>
<p>Address: <input type = "text" size="50" maxlength = "100" name="address" value = "<?php echo $_POST['address'];?>"></p>
<p>City: <input type = "text" size="50" maxlength = "50" name="city" value ="<?php echo $_POST['city'];?>"></p>
<p>Province or State: <input type = "text" size="50" maxlength = "50" name="provstate" value="<?php echo $_POST['provstate'];?>"></p>
<p>Country: <select name = "country" value ="<?php echo $_POST['country'];?>">
<option> </option>
<option>CA</option>
<option>US</option>
</select>
</p>
<p>Telephone: <input type = "text" size="20" maxlength = "20" name="tel" value= "<?php echo $_POST['tel'];?>"></p>
<p>Email address: <input type = "text" size="75" maxlength = "75" name ="email" value = "<?php echo $_POST['email'];?>"></p>
<p><input type = "button" name = "submit" value = "Submit"></p>
</form>
</font>
</body>
</html>