codestudent
codestudent

Reputation: 73

Why isn't this PHP code validating user input?

I am creating a form with a validation code. I already know the form works as I'm building off of a previous project by adding the validation code. I am doing this for a class and I followed exactly what the example showed. For whatever reason though, it isn't validating. I don't need someone to tell me how to do my homework, but I could really use a point in the right direction here.

The code below is from my HTML file which, when the form is submitted, passes the data through a PHP file. It's only the validation code in the HTML file that I'm struggling with, so I won't attach the PHP file.

<!Doctype html>
<html>
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="basics.css"/>
<title>Form Process</title>
</head>
<body>
<div id="holder">
  <?php
  $fname=$_POST['fname'];
  $lname=$_POST['lname'];
  $street=$_POST['street'];
  $city=$_POST['city'];
  $state=$_POST['state'];
  $zip=$_POST['zip'];
  $phone=$_POST['phone'];
  if (empty($fname)) {
    $error_message="You must enter a value for your first name!";
  } else if (empty($lname)) {
    $error_message="You must enter a value for your last name!";
  } else if (empty($street)) {
    $error_message="You must enter a value for your street address!";
  } else if (empty($city)) {
    $error_message="You must enter a value for your city!";
  } else if (empty($state)) {
    $error_message="You must enter a value for your state!";
  } else if (empty($zip)) {
    $error_message="You must enter a value for your zip code!";
  } else if (empty($phone)) {
    $error_message="You must enter a value for your phone number!";
  } else {
    $error_message="You must enter a value!";
  }
?>
<h1>Sign Up Now!</h1>
<center><form action="info_output_includes.php" method="post">
<table>
<tr><td>First Name:</td><td><input type="text" id="fname" name="fname"/></td></tr>
<tr><td>Last Name:</td><td><input type="text" id="lname" name="lname"/></td></tr>
<tr><td>Address:</td><td><input type="text" id="street" name="street" /></td></tr>
<tr><td>City:</td><td><input type="text" id="city" name="city" /></td></tr>
<tr><td>State:</td><td><input type="text" id="state" name="state"/></td></tr>
<tr><td>Zip Code:</td><td><input type="text" id="zip" name="zip" /></td></tr>
<tr><td>Phone:</td><td><input type="text" id="phone" name="phone"/></td></tr>
<tr ><td ><input type="submit" value="Submit" /></td>
<td class="totheleft"><input type="reset" value="Clear" /></td></tr>
</table>
</form></center>
<br>
<a href="index.php">Home</a></p>
</div>
</body>
</html>

If the validation code were working, it should say (for example) "You must enter a value for your first name!" if the user submitted the form without entering a first name value. Of course, it should basically print an error message if any value is left empty.

Upvotes: 0

Views: 29

Answers (1)

Dimitris Damilos
Dimitris Damilos

Reputation: 2438

The reason you're not seeing the error is because you're not outputting it somewhere. You need to add a line like:

<?php echo $error_message; ?>

Restructuring the code in a more logical way using only HTML like you have it, would be:

<?php
  $fname=$_POST['fname'];
  $lname=$_POST['lname'];
  $street=$_POST['street'];
  $city=$_POST['city'];
  $state=$_POST['state'];
  $zip=$_POST['zip'];
  $phone=$_POST['phone'];

  if (empty($fname)) {
    $error_message="You must enter a value for your first name!";
  } else if (empty($lname)) {
    $error_message="You must enter a value for your last name!";
  } else if (empty($street)) {
    $error_message="You must enter a value for your street address!";
  } else if (empty($city)) {
    $error_message="You must enter a value for your city!";
  } else if (empty($state)) {
    $error_message="You must enter a value for your state!";
  } else if (empty($zip)) {
    $error_message="You must enter a value for your zip code!";
  } else if (empty($phone)) {
    $error_message="You must enter a value for your phone number!";
  } else {
    $error_message="You must enter a value!";
  }
?>

<!DOCTYPE html>
<html lang="en" dir="lrt">
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="basics.css"/>
    <title>Form Process</title>
</head>
<body>
    <div id="holder">
        <h1>Sign Up Now!</h1>
        <div style="color:red;"><?php echo $error_message; ?></div>
        <center>
            <form name="input-form" action="info_output_includes.php" method="post">
                <table>
                <tr><td>First Name:</td><td><input type="text" id="fname" name="fname"/></td></tr>
                <tr><td>Last Name:</td><td><input type="text" id="lname" name="lname"/></td></tr>
                <tr><td>Address:</td><td><input type="text" id="street" name="street" /></td></tr>
                <tr><td>City:</td><td><input type="text" id="city" name="city" /></td></tr>
                <tr><td>State:</td><td><input type="text" id="state" name="state"/></td></tr>
                <tr><td>Zip Code:</td><td><input type="text" id="zip" name="zip" /></td></tr>
                <tr><td>Phone:</td><td><input type="text" id="phone" name="phone"/></td></tr>
                <tr ><td ><input type="submit" value="Submit" /></td>
                <td class="totheleft"><input type="reset" value="Clear" /></td></tr>
                </table>
            </form>
        </center>
        <br>
        <a href="index.php">Home</a></p>
    </div>
</body>
</html>

Upvotes: 1

Related Questions