1

I have been following the w3schools php tutorial to produce a form. The form collects and displays data but none of the error checking is working. I have run the php and html through lint with no errors and double checked my use of quotes.

Specifically the script is not applying htmlspecialchars, trim or split. None of the regex checking is being applied and the error messages are not appearing. I tried replacing an error message with a simple if / not echo message and that worked, although the message was placed at the head of the web page, not next to the field.

I am also getting the following on execution.

Notice: Undefined index: numChild in /volume1/homes/richard/www/action.php on line 22

Line 22 is:

$totalGuests = $_POST['numAdults'] =+ $_POST['numChild'];

The difference between this and other undefined index questions is that the function 'test_input' is not being applied. I have included @Poiz suggestion below and this resolves the undefined index problem but the function is still now working.


My code is:

<!DOCTYPE html>
<html lang="en">

<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

// define variables and set to empty values
$guestName = $guestEmail = $guestPhone = $startDate = $endDate = $numAdults = $numChild = "";
$guestNameErr = $guestEmailErr = $guestPhoneErr = $startDateErr = $endDateErr = $numAdultsErr = $numChildErr = "";

$maxAllowed = 4;
$totalGuests = $_POST['numAdults'] =+ $_POST['numChild'];

if ($_SERVER['REQUEST_METHOD'] == "POST") {

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

    if (empty($_POST['guestEmail'])) {
    $guestEmailErr = "Email is required";
    } else {
    $guestEmail = test_input($_POST['guestEmail']);
    // check if e-mail address is well-formed
    if (!filter_var($guestEmail, FILTER_VALIDATE_EMAIL)) {
      $guestEmailErr = "Invalid guest email format";
      }
    }  

    if (empty($_POST['guestPhone'])) {
    $guestPhoneErr = "Phone number is required";
    } else {
    $guestPhone = test_input($_POST['guestPhone']);
    // check if phone number is UK format and valid
    if (!preg_match("/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/",$guestPhone)) {
        $guestPhoneErr = "Please enter a valid phone number";
      }
    }

    if (empty($_POST['startDate'])) {
    $startDateErr = "Start date is required";
    } else {
    $startDate = $_POST['startDate'];
    }

    if (empty($_POST['endDate'])) {
    $endDateErr = "Start date is required";
    } else {
      $endDate = $_POST['endDate'];
      if ($_POST['endDate'] < $_POST['startDate']) {
        $sendDateErr = "End date must be before start date";
        }
    }

    if ($totalGuests > $maxAllowed) {
    $bookingErr = "A maximum of 4 guests can be accommodated at Mariner's Loft";
    } else {
        $numAdults = ($_POST['numAdults']);
        $numChild = ($_POST['numChild']);
        }
}

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

?>

 <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">


    <br><input type="text" class="form-control" id="guestName" name="guestName" value="<?php echo $guestName;?>" placeholder="Full name" autofocus required>
    <br>
    <br><input type="email" class="form-control" id="guestEmail" name="guestEmail" value="<?php echo $guestEmail;?>" placeholder="email">
    <br>
    <br><input type="phone" class="form-control" id="guestPhone" name="guestPhone" value="<?php echo $guestPhone;?>" placeholder="Phone number">

    <p><strong>How many guests?</strong></p>

    <p>How many adults?</p>
    <br><input type="text" class="form-control" id="numAdults" name="numAdults" value="<?php echo $numAdults;?>" placeholder="number of adults">
    <p>How many children?</p>
    <br><input type="text" class="form-control" id="numChild" name="numChild" value="<?php echo $numChild;?>" placeholder="number of children">
    <br>
    <br><input type="date" class="form-control" id="startDate" name="startDate" value="<?php echo $startDate;?>" placeholder="Arrival date">
    <br>
    <br><input type="date" class="form-control" id="endDate" name="endDate" value="<?php echo $endDate;?>" placeholder="Leaving date">
    <br><br>
    <input type="submit" name="submit" value= "Submit"/>
    <br><br>
    <input type="reset" name="submit" value="Cancel"/>
</form>

<?php

    echo "<h2>Guest info:</h2>";
    echo $guestName;
    echo "<br>";
    echo $guestEmail;
    echo "<br>";
    echo $guestPhone;
    echo "<br>";
    echo $totalGuests;
    echo "<br>";
    echo $startDate;
    echo "<br>";
    echo $endDate;
?>
</body>
</html>

Any help gratefully received.

sidestrand
  • 71
  • 12
  • Side note: If you're trying to add and assign at the same time, then `=+` does not do what you expect. I think you might want `+=` – Jeff Puckett Oct 26 '16 at 15:07
  • @JeffPuckettII in this case it looks like it's to cast the post value to a number, which is what `= +$numericString` would do. Though it may not be the intent. – apokryfos Oct 26 '16 at 15:16

1 Answers1

1

The Code $totalGuests = $_POST['numAdults'] =+ $_POST['numChild']; should be moved into the Conditional Block or redeclared to have a Default: (1, for example)...

   $maxAllowed  = 4;
   $totalGuests = ( isset($_POST['numAdults'] ) &&
                    isset($_POST['numChild']) ) ?
                    ( $_POST['numAdults'] + $_POST['numChild'] )
                    :1;
   if ($_SERVER['REQUEST_METHOD'] == "POST") {
      // REST OF THE CODES
   }
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • But, if you do that, he will get error on line 124, because $totalGuests won't exist (`echo $totalGuests;`). So, keep the variable initialization where it was, but give it the value `""`. – Jose Manuel Abarca Rodríguez Oct 26 '16 at 15:05
  • I applied @Poiz suggestion and the script runs without error messages, and totalGuests appears as it should. The validation function 'test_input' still does not work. – sidestrand Oct 26 '16 at 17:13