Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am very new to PHP and somehow got thrown into an advanced level course. My instructor will not offer any help since it is an advanced course. I will post my code below in hopes that someone can at least point me in the right direction for a fix. This is only the first part of the assignment, but not worth going any further if I cannot get this part to work. I believe I have check all the braces and quotes, but I sure could have missed something or ?????.

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
// define variables and set to empty values
$name1Err = $name2Err = $dobErr = $stateErr = $zipErr = "";
$name1 = $name2 = $dob = $state = $zip = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   if (empty($_POST["name1"]))
     {$name1Err = "First Name is required";}
   else
     {
     $name1 = test_input($_POST["name1"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name1))
       {
       $name1Err = "Only letters and white space allowed";
       }
     }

   if (empty($_POST["name2"]))
     {$name2Err = "Name is required";}
   else
     {
     $name2 = test_input($_POST["name2"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name2))
       {
       $name2Err = "Only letters and white space allowed";
       }
     }

   if (empty($_POST["dob"]))
     {$dobErr = "Date Of Birth is required";}
   else
     {
     $dob = test_input($_POST["dob"]);
     // check if date of birth format is valid
     if (!preg_match("/^[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}$/",$dob))
       {
       $dobErr = "Invalid date format";
       }
     }

   if (empty($_POST["state"]))
     {$stateErr = "2 Digit State Abbreviation is required";}
   else
     {
     $state = test_input($_POST["state"]);
     // match two uppercase letters with definite (one or more) whitespace on either side)
     if (!preg_match("/\s+[A-Z]{2}\s+/ ",$state))
       {
       $stateErr = "Invalid State Abbreviation";
   }
     }

   if (empty($_POST["zip"]))
     {$zipErr = "Zip Code is required";}
   else
     {
     $zip = test_input($_POST["zip"]);
     // Validates Zip Code (5 digits plus optional -4)
     if (!preg_match("^\d{5}(-\d{4})?$",$zip))
       {
       $zipErr = "Only letters and white space allowed";
       }
     }


function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>

<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
   First Name: 
     <input type="text" name="name1">
   <span class="error">* <?php echo $name1Err;?></span>
   <br><br>
   Last Name: 
   <input type="text" name="name2">
   <span class="error">* <?php echo $name2Err;?></span>
   <br><br>
   Date Of Birth: 
   <input type="text" name="dob">
   <span class="error"><?php echo $dobErr;?></span>
   <br><br>
   State: 
     <input type="text" name="state">
   <span class="error">* <?php echo $stateErr;?></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>

<?php
echo "<h2>Your Input:</h2>";
echo $name1;
echo "<br>";
echo $name2;
echo "<br>";
echo $dob;
echo "<br>";
echo $state;
echo "<br>";
echo $zip;
?>
</body>

</html>
share|improve this question
    
Check your open/close braces in your two code sections, make sure they line up. – Joe Nov 2 '13 at 18:30
    
Lesson #1: Pick a consistent style for braces and indentation and stick to it. This minimizes these kinds of errors tremendously. – deceze Nov 2 '13 at 18:34
    
possible duplicate of Reference - What does this error mean in PHP? – HamZa Nov 2 '13 at 18:57

You are missing to close you if.. statement.

 if ($_SERVER["REQUEST_METHOD"] == "POST")
 {  
    // your code
 }
^^^

Use Better Text Editor to get rid from this type of SYNTAX errors

share|improve this answer
1  
thanks .. it really helped me – geek_sandeep Jul 5 '15 at 18:51

You forgot to close a brace at the end

Try this

function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
}// <------------ here
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.