【问题标题】:Can't validate and display errors in PHP无法验证和显示 PHP 中的错误
【发布时间】:2017-09-15 17:32:54
【问题描述】:

我正在尝试显示带有运输信息标题的表单信息,并验证姓名、联系方式等。我能够显示它但无法验证它,如果我提交一个空白的表单,它会显示除付款和运输之外的空白值方法显示时选择了默认值,还有一件事没有显示错误,即; fname 是必需的,我有点困惑。任何人都可以添加一件事,如果有人发布空白表单,它会再次将其重定向到 form2.php 页面?

这是我的 form2.php 页面

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    <form action="welcome2.php" method="post">


        <label>First name: </label>
        <input type="text" name="fname"><br>
        <label>Lastname name: </label>
        <input type="text" name="lname"><br>
        <label>Email: </label>
        <input type="email" name="email"><br>
        <label>Contact No </label>
        <input type="text" name="cno"><br>
        <label>Address </label>
        <input type="text" name="addr"><br>
        <label>City </label>
        <input type="text" name="city"><br>
        <label>State </label>
        <input type="text" name="state"><br>
        <label>Country </label>
        <input type="text" name="country"><br>
        <label>Zip Code </label>
        <input type="text" name="zipcode"><br>
        <label>Credit Card Number </label>
        <input type="text" name="ccno"><br>
        <label>Payment Option </label>
        <select name="Payment_option">
    <option value="Cash On Delivery">Cash On Delivery</option>
    <option value="Online">Online</option>
  </select> <br>
  <label>Shipping Method </label>
        <select name="Shipping_Method">
    <option value="TCS">TCS</option>
    <option value="Leapord">Leapord</option>
    <option value="FEDEX">FEDEX</option>
  </select> <br>
        <button type="submit" name="sub">Submit</button>

    </form>
    <?php
    if(isset($_SESSION['errors']))
    {
        foreach($_SESSION['errors'] as $key => $error)
        {
            echo $error."<br>";
        }
        unset($_SESSION['errors']);
    }
    ?>
</body>
</html>

这是我的welcome2.php页面

    <?php  session_start();
    $fname = "";
    $lname = "";
    $email = "";
    $cno = "";
    $addr = "";
    $city = "";
    $state = "";
    $country = "";
    $zipcode = "";
    $ccno = "";
    extract($_POST);
    $errors = array();
    if(isset($_POST['fname'])){
        $_SESSION['fname'] = $_POST['fname'];
    }if(isset($_POST['lname'])){
        $_SESSION['lname'] = $_POST['lname'];
    }if(isset($_POST['email'])){
        $_SESSION['email'] = $_POST['email'];
    }if(isset($_POST['cno'])){
        $_SESSION['cno'] = $_POST['cno'];
    }if(isset($_POST['addr'])){
        $_SESSION['addr'] = $_POST['addr'];
    }if(isset($_POST['city'])){
        $_SESSION['city'] = $_POST['city'];
    }if(isset($_POST['state'])){
        $_SESSION['state'] = $_POST['state'];
    }if(isset($_POST['country'])){
        $_SESSION['country'] = $_POST['country'];
    }if(isset($_POST['zipcode'])){
        $_SESSION['zipcode'] = $_POST['zipcode'];
    }if(isset($_POST['ccno'])){
        $_SESSION['ccno'] = $_POST['ccno'];
    }
    if(isset($_POST['sub']))
    {

    if(!$fname)
        $errors[] = "First name is required";
    }

    if(!$lname)
    {
        $errors[] = "Last name is required";
    }
    if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
      $errors = "Only letters and white space allowed"; 
    }
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
      $errors = "Only letters and white space allowed"; 
    }
    if(!$email)
    {
        $errors[] = "Email is required";
    }
    if(!$cno)
    {
        $errors[] = "Contact  is required";
    }if (strlen($cno)<=5)
    {
        $errors[] ="Contact contain more than 11 characters";
    }
    if(!$addr)
    {
        $errors[] = "Address is required";
    }
    if(!$city)
    {
        $errors[] = "City is required";
    }
    if(!$state)
    {
        $errors[] = "State is required";
    }
    if(!$country)
    {
        $errors[] = "Country is required";
    }
    if(!$zipcode)
    {
        $errors[] = "Zip Code is required";
    }
    if(!$ccno)
    {
        $errors[] = "Credit Card Number is required";
    }?>
    <html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h1> Shipping Information </h1>
<?php   echo $fname; echo "<br>";
    echo $lname; echo "<br>";
     echo $email; echo "<br>";
     echo $cno; echo "<br>";
    echo $addr; echo "<br>";
    echo $city; echo "<br>";
    echo $state; echo "<br>";
    echo $country; echo "<br>";
     echo $zipcode; echo "<br>";
     echo $ccno; echo "<br>";
    $option1 = isset($_POST['Payment_option']) ? $_POST['Payment_option'] : false;
   if ($option1) {
      echo htmlentities($_POST['Payment_option'], ENT_QUOTES, "UTF-8");
   } else {
     echo "Payment Method is required";
   } echo "<br>";
   $option2 = isset($_POST['Shipping_Method']) ? $_POST['Shipping_Method'] : false;
   if ($option2) {
      echo htmlentities($_POST['Shipping_Method'], ENT_QUOTES, "UTF-8");
   } else {
     echo "Shipping Method is required";
   }
   ?> 

【问题讨论】:

    标签: php forms validation session


    【解决方案1】:

    您在 PHP 文件的顶部设置了值,因此它们总是被设置。然后错误永远不会触发,因为您没有检查 $_POST[&lt;value&gt;] 是否已设置,然后将其分配给变量。

    另外,尝试使用循环。它有助于代码维护和可读性。像这样的东西可能对你有用:

    $values = [
    'fname'     =>  'First Name',
    'lname'     =>  'Last Name',
    'email'     =>  'Email',
    'test'      =>  'testt',
    ];
    $results = array();
    $errors = array();
    foreach ( $values as $name => $displayName ) {
        if ( isset( $_POST[ $name ] ) )
        {
            $results[ $name ] = $_POST[ $name ];
        }
        else
        {
            $errors[] = $displayName . ' is required';
        }
    }
    

    【讨论】:

    • @SyedKhizer 您是否获取了所有表单数据并正确显示错误?
    【解决方案2】:

    您的错误是通过错误检查添加的。使用 empty($variable) 而不是 NOT 运算符 (!)。

    另外,请注意如何更改原始空变量的值。我会使用 extract( $_POST, EXTR_OVERWRITE, 'form_' ); 更安全,然后通过 $form_fname 等引用。然后使用 empty() 检查。

    我还建议您使用可以逐步检查每一行的调试器。它创造了奇迹。

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 1970-01-01
      • 2012-12-07
      • 2016-10-13
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多