Jump to content

Recommended Posts

Below is my code  and the end of the program is all my problems.... I hopeful its only a problem but knowing my luck its more than just one thing...

<?php
//Validation functions are located in a separate file so they can be reused
//on several php pages.
session_start();

include_once('ValidationUtilities.php');

//retrieve values from form
$firstName = filter_input(INPUT_GET,'firstName');
$lastName = filter_input(INPUT_GET,'lastName');
$age  = filter_input(INPUT_GET,'age');
$email  = filter_input(INPUT_GET,'email');
$state  = filter_input(INPUT_GET,'state');


?>
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Validate User Inputs</title>
   <link href="styles_validate.css" rel="stylesheet" type="text/css">
  

</head>

<body>
<div class="pageContainer">

<h2>Validate User Inputs</h2><hr>
<?php
    $pageValid = true;
    $postback = $_GET['postback'];
    if ($pageValid == true && $postback)
?>
   <form>
          <?php
          $firstName = $_GET['firstName'];
          ?>
      Please enter your first name:<br>
     <input type="text" name="firstName" size="25" value="<?php echo "$firstName"; ?>"><br>
          <?php
          echo "" . fValidateName($firstName) . "";
          ?>
   
    <?php
          $lastName= $_GET['lastName'];
          ?>
      Please enter your last name:<br>
     <input type="text" name="lastName" size="25" value="<?php echo "$lastName"; ?>"><br>
          <?php
          echo "" . lValidateName($lastName) . "";
          ?>
  

          <?php
          $age= $_GET['age'];
          ?>
      Please enter your age (13-99):<br>
     <input type="text" name="age" size="5" value="<?php echo "$age"; ?>"><br>
          <?php
          echo "" . fIsAge($age) . "";
          ?>
    


          <?php
          $email= $_GET['email'];
          ?>
      Please enter your email address:<br>
     <input type="text" name="email" size="25" value="<?php echo "$email"; ?>"><br>
          <?php
          echo "" . fIsEmail($email) . "";
          ?>
     

          <?php
          $state= $_GET['state'];
          ?>
      Please enter your state abbreviation:<br>
     <input type="text" name="state" size="2" value="<?php echo "$state"; ?>"><br>
          <?php
          echo "" . fIsStateAbbr($state) . "";
          ?>
     

      <input type="hidden" name="postback" value="true">
      <input type="submit" value="Submit your info!">
  </form>

  <?php
      //$PageIsValid$PageIsValid is a flag to indicate if all form items are valid
      if($IsPostBack && $IsValidPage)
         echo "<h3>Success</h3>";
      ?>
     
      <p><a href="Validate.php"?">Reload page</a></p>
     
   </div>
</body>
</html>

 

<?php
// Validation functions
// All functions return true or false


//Name Validation
function fValidateName($firstName) {
    if ($postback && strlen($firstName) == 0) {
    echo "<font color='red'>Please enter your first name (alpha characters only)</font>";
    $pageValid = false;
    }
}
function lValidateName($lastName) {
    if ($postback && strlen($lastName) == 0) {
    echo "<font color='red'>Please enter your last name (alpha characters only)</font>";
    $pageValid = false;
    }
}
//Age Validation
function fIsAge($age) {
    if ($postback && ($age<13||$age>99)||strlen($age) == 0) {
    echo "<font color='red'>Please enter a valid age (13-99) </font>";
    $pageValid = false;
    }
}
//Email Validation
function fIsEmail($email) {
    $pattern = "^[\w!#$%*_\-/?|\^\{\}'~\.]+@(\w+\.)+\w{2,4}^";
    if ($postback && !(preg_match($pattern, $email))) {
    echo "<font color='red'>Please enter a valid email address (xx@xx.xxx)</font>";
    $pageValid = false;
    }
}
//State Validation
function fIsStateAbbr($state){
$ValidAbbr = array("AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL",
    "IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH",
    "NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT",
    "VA","WA","WV","WI","WY");

    $state = strtoupper($state);
    //in_array() checks if a value exists in an array.
    if (in_array($state, $ValidAbbr)) {
    } else {echo "<font color='red'>Please enter a valid state abbreviation</font>";
    $pageValid = false;
    }
}

?>

 

then my output is:

Validate User Inputs


Notice: Undefined index: postback in C:\xampp\htdocs\web3\validate.php on line 33

Top of Form


Notice: Undefined index: firstName in C:\xampp\htdocs\web3\validate.php on line 38
Please enter your first name:


Notice: Undefined variable: postback in C:\xampp\htdocs\web3\ValidationUtilities.php on line 9

Notice: Undefined index: lastName in C:\xampp\htdocs\web3\validate.php on line 47
Please enter your last name:


Notice: Undefined variable: postback in C:\xampp\htdocs\web3\ValidationUtilities.php on line 15

Notice: Undefined index: age in C:\xampp\htdocs\web3\validate.php on line 57
Please enter your age (13-99):


Notice: Undefined variable: postback in C:\xampp\htdocs\web3\ValidationUtilities.php on line 22
Please enter a valid age (13-99)
Notice: Undefined index: email in C:\xampp\htdocs\web3\validate.php on line 68
Please enter your email address:


Notice: Undefined variable: postback in C:\xampp\htdocs\web3\ValidationUtilities.php on line 30

Notice: Undefined index: state in C:\xampp\htdocs\web3\validate.php on line 78
Please enter your state abbreviation:

Please enter a valid state abbreviation

Bottom of Form


Notice: Undefined variable: IsPostBack in C:\xampp\htdocs\web3\validate.php on line 93

Reload page

you have got no conditional logic around any of your get variables, so when you first load the form none of these will exist. you need to do something along the lines of:

 <?php
if(array_key_exists('firstName', $_GET)){
    $firstName = $_GET['firstName'];
}
 ?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.