sidestrand sidestrand - 1 year ago 54
PHP Question

php function not being applied

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

. 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">

.error {color: #FF0000;}


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 (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><input type="email" class="form-control" id="guestEmail" name="guestEmail" value="<?php echo $guestEmail;?>" placeholder="email">
<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><input type="date" class="form-control" id="startDate" name="startDate" value="<?php echo $startDate;?>" placeholder="Arrival date">
<br><input type="date" class="form-control" id="endDate" name="endDate" value="<?php echo $endDate;?>" placeholder="Leaving date">
<input type="submit" name="submit" value= "Submit"/>
<input type="reset" name="submit" value="Cancel"/>


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;

Any help gratefully received.

Answer Source

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'] )
   if ($_SERVER['REQUEST_METHOD'] == "POST") {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download