php函数未被应用

时间:2016-10-26 14:49:09

标签: php html

我一直在关注w3schools php教程来制作表格。表单收集并显示数据,但没有任何错误检查工作。我通过lint运行了php和html,没有任何错误,并仔细检查了我对引号的使用。

具体而言,该脚本未应用htmlspecialcharstrimsplit。没有正在应用正则表达式检查,并且没有出现错误消息。我尝试使用简单的if / not echo消息替换错误消息并且这样做有效,尽管消息放在网页的头部,而不是在字段旁边。

我也在执行时获得以下内容。

  

注意:未定义索引:第22行/volume1/homes/richard/www/action.php中的numChild

第22行是:

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

此与未定义的索引问题之间的区别在于函数'test_input'未应用。我在下面列出了@Poiz建议,这解决了未定义的索引问题,但该功能现在仍然有效。

我的代码是:

<!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>

感激不尽的任何帮助。

1 个答案:

答案 0 :(得分:1)

  

应将代码$totalGuests = $_POST['numAdults'] =+ $_POST['numChild'];移入条件阻止或重新声明以具有默认值:(例如1)......

   $maxAllowed  = 4;
   $totalGuests = ( isset($_POST['numAdults'] ) &&
                    isset($_POST['numChild']) ) ?
                    ( $_POST['numAdults'] + $_POST['numChild'] )
                    :1;
   if ($_SERVER['REQUEST_METHOD'] == "POST") {
      // REST OF THE CODES
   }