I can't seem to get this form to work properly. My web scripting knowledge is pretty limited as I'm still a student. I searched and found this Post Self Form Validation and Submission in PHP which is the same assignment but this person had a different problem.
For some reason when I try to test out my page "> Name: shows up as my first line but the "> is the closing for the html form opening tag.
I also have this a .html because I want to submit it as one document and not have the PHP code in a separate .php document.
edit: @ Quentin - This is not related to the duplicate article that this was marked as. I'm not running an Apache server. That person solved his problem by restarting the XAMPP service I'm Windows 10 and cannot possibly have that service running.
I want it to run off one document, thus why I was using PHP_SELF. When I save the file as a .php it doesn't do anything but display the code.
I did inspect the code in Chrome, the only way I know how to debug it. And I couldn't find anything wrong with it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Assignment 6.2 PHP Validation</title>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $addressErr = $phoneErr = $zipErr = "";
$name = $address = $phone = $zip = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
//check name field
if (empty($_POST["address"])) {
$emailErr = "Address is required";
}
// check phone number field
if (empty($_POST['phone'])) {
$phoneErr = "Phone number is required";
} else {
$phone = test_input($_POST['phone']);
if(!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/", $phone)) {
$phoneErr = "must be in ddd-ddd-dddd format";
}
}
//check zip code field
if (empty($_POST['zip'])) {
$zipErr = "Zip code is required";
} else {
$zip = test_input($_POST['zip']);
if(!preg_match("/^[0-9]{5}-[0-9]{4}$/", $)) {
$zipErr = "Zip code must be in ddddd-dddd format";
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Address:
<input type="text" name="address">
<span class="error">* <?php echo $addressErr;?></span>
<br><br>
Phone Number:
<input type="text" name="phone">
<span class="error"><?php echo $phoneErr;?></span>
<br><br>
Zip Code:
<input type="text" name="zip">
<span class="error"><?php echo $zipErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>