Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让php验证在表单上工作?_Php_Html_Forms_Validation_Session - Fatal编程技术网

如何让php验证在表单上工作?

如何让php验证在表单上工作?,php,html,forms,validation,session,Php,Html,Forms,Validation,Session,我需要在将表单发送到会话数组之前对其进行验证,这就是我目前所拥有的 使用PHP\u SELF将表单发送到需要验证输入正确有效的索引 提交表单前会回显错误,每次提交时输入错误,其他消息不会回显。页面刷新 任何帮助都将是惊人的,我已经禁用了javascript验证来测试这一点,并且是半功能性的 PHP代码: <?php // define variables and set to empty values $nameErr = $emailErr = $mobileErr =

我需要在将表单发送到会话数组之前对其进行验证,这就是我目前所拥有的

使用
PHP\u SELF
将表单发送到需要验证输入正确有效的索引

提交表单前会回显错误,每次提交时输入错误,其他消息不会回显。页面刷新

任何帮助都将是惊人的,我已经禁用了javascript验证来测试这一点,并且是半功能性的

PHP代码:

<?php
    // define variables and set to empty values
    $nameErr = $emailErr = $mobileErr = $creditCardErr = $expiryErr = "";
    $name = $email = $mobile = $creditCard = $expiry = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        function validate($str) {
            return trim(htmlspecialchars($str));
        }

        //Valikdating User name
        if (empty($_POST["cust[name]"])) {
            $nameErr = "Name is required";
        } else {
            $name = validate($_POST["cust[name]"]);

            if (!preg_match("/\b([A-Z]{1}[a-z]{1,30}[- ]{0,1}|[A-Z]{1}[- \']{1}[A-Z]{0,1}  
    [a-z]{1,30}[- ]{0,1}|[a-z]{1,2}[ -\']{1}[A-Z]{1}[a-z]{1,30}){2,5}/",$name)) {
                $nameErr = "Only Western names, letters and white spaces allowed";
            }
        }

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

        if (empty($_POST["cust[mobile]"])) {
            $mobileErr = "Mobile Number is required";
        } else {
            $mobile = validate($_POST["cust[mobile]"]);
            //check if mobile phone is inputted correctly
            if (!preg_match("/^(\(04\)|04|\+614)( ?\d){8}$/",$mobile)) {
                $mobilelErr = "Invalid Mobile Number";
            }
        }

        if (empty($_POST["cust[card]"])) {
            $creditCardErr = "Credit Card is required";
        } else {
            $creditCard = validate($_POST["cust[card]"]);
            //check if all credit card types are valid

            if (!preg_match("/^(\d){16}$/",$creditCard)) {

            }else if(!preg_match("/^(\d){4}\s{1}(\d){4}\s{1}(\d){4}\s{1}(\d){4}$/",$creditCard)) {

            }else if(!preg_match("/^(\d){4}\-{1}(\d){4}\-{1}(\d){4}\-{1}(\d){4}$/",$creditCard)) {
                $creditCardErr = "Invalid Credit Card";
            }
        }




        //This needs to be fixed
       if (empty($_POST["cust[expiry]"])) {
           $expiryErr = "Expiry is required";
       } else {
           $expiry = validate($_POST["cust[expiry]"]);
           //check if expiry is valid and a month ahead
           if (!preg_match("",$expiry)) {
               $expiryErr = "Invalid date, select month onwards";
           }
       }
   }
<div id="bookingsCard">
    <form  id="seatform" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

        <!-- Customer Details -->
        <div class="CustomerDetails">

            <label for="CustomerDetails"><b>CUSTOMER DETAILS</b></label>
            <br><br>

            <label for="Name"><b>Name</b></label>
            <input type="text" name="cust[name]"  id="cust-name" value="<?php echo $name; ?>">
            <span class="error">* <?php echo $nameErr;?></span>
            <br><br>

            <label for="Email"><b>Email</b></label>
            <input type="email" name="cust[email]" id='cust-email' value="<?php echo $email; ?>">
            <span class="error">* <?php echo $emailErr;?></span>
            <br><br>

            <label for="Mobile"><b>Mobile</b></label>
            <input type="tel" name="cust[mobile]" id='cust-mobile' value="<?php echo $mobile; ?>">
            <span class="error">* <?php echo $mobileErr;?></span>
            <br><br>

            <label for="Credit Card"><b>Credit Card</b></label>
            <input type="text" name="cust[card]" id='cust-card' value="<?php echo $creditCard; ?>">
            <span class="error">* <?php echo $creditCardErr;?></span>
            <br><br>

            <label for="Expiry"><b>Expiry</b></label>
            <input type="month" name="cust[expiry]" id='cust-expiry' 
                                            pattern="[0-9]{4}-[0-9]{2}" value="<?php echo $expiry; ?>">
            <span class="error">* <?php echo $expiryErr;?></span>
            <br><br>
        </div>
        <hr>
        <label><b>Total Amount</b></label>
        <input  name="total" id="total" type="text" readonly>
        <br><br>
        <button type="submit" class="bookbtn" name="order" value='order' id='bookbtn' >Book Now!</button>
    </form>
</div>

HTML表单代码:

<?php
    // define variables and set to empty values
    $nameErr = $emailErr = $mobileErr = $creditCardErr = $expiryErr = "";
    $name = $email = $mobile = $creditCard = $expiry = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        function validate($str) {
            return trim(htmlspecialchars($str));
        }

        //Valikdating User name
        if (empty($_POST["cust[name]"])) {
            $nameErr = "Name is required";
        } else {
            $name = validate($_POST["cust[name]"]);

            if (!preg_match("/\b([A-Z]{1}[a-z]{1,30}[- ]{0,1}|[A-Z]{1}[- \']{1}[A-Z]{0,1}  
    [a-z]{1,30}[- ]{0,1}|[a-z]{1,2}[ -\']{1}[A-Z]{1}[a-z]{1,30}){2,5}/",$name)) {
                $nameErr = "Only Western names, letters and white spaces allowed";
            }
        }

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

        if (empty($_POST["cust[mobile]"])) {
            $mobileErr = "Mobile Number is required";
        } else {
            $mobile = validate($_POST["cust[mobile]"]);
            //check if mobile phone is inputted correctly
            if (!preg_match("/^(\(04\)|04|\+614)( ?\d){8}$/",$mobile)) {
                $mobilelErr = "Invalid Mobile Number";
            }
        }

        if (empty($_POST["cust[card]"])) {
            $creditCardErr = "Credit Card is required";
        } else {
            $creditCard = validate($_POST["cust[card]"]);
            //check if all credit card types are valid

            if (!preg_match("/^(\d){16}$/",$creditCard)) {

            }else if(!preg_match("/^(\d){4}\s{1}(\d){4}\s{1}(\d){4}\s{1}(\d){4}$/",$creditCard)) {

            }else if(!preg_match("/^(\d){4}\-{1}(\d){4}\-{1}(\d){4}\-{1}(\d){4}$/",$creditCard)) {
                $creditCardErr = "Invalid Credit Card";
            }
        }




        //This needs to be fixed
       if (empty($_POST["cust[expiry]"])) {
           $expiryErr = "Expiry is required";
       } else {
           $expiry = validate($_POST["cust[expiry]"]);
           //check if expiry is valid and a month ahead
           if (!preg_match("",$expiry)) {
               $expiryErr = "Invalid date, select month onwards";
           }
       }
   }
<div id="bookingsCard">
    <form  id="seatform" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

        <!-- Customer Details -->
        <div class="CustomerDetails">

            <label for="CustomerDetails"><b>CUSTOMER DETAILS</b></label>
            <br><br>

            <label for="Name"><b>Name</b></label>
            <input type="text" name="cust[name]"  id="cust-name" value="<?php echo $name; ?>">
            <span class="error">* <?php echo $nameErr;?></span>
            <br><br>

            <label for="Email"><b>Email</b></label>
            <input type="email" name="cust[email]" id='cust-email' value="<?php echo $email; ?>">
            <span class="error">* <?php echo $emailErr;?></span>
            <br><br>

            <label for="Mobile"><b>Mobile</b></label>
            <input type="tel" name="cust[mobile]" id='cust-mobile' value="<?php echo $mobile; ?>">
            <span class="error">* <?php echo $mobileErr;?></span>
            <br><br>

            <label for="Credit Card"><b>Credit Card</b></label>
            <input type="text" name="cust[card]" id='cust-card' value="<?php echo $creditCard; ?>">
            <span class="error">* <?php echo $creditCardErr;?></span>
            <br><br>

            <label for="Expiry"><b>Expiry</b></label>
            <input type="month" name="cust[expiry]" id='cust-expiry' 
                                            pattern="[0-9]{4}-[0-9]{2}" value="<?php echo $expiry; ?>">
            <span class="error">* <?php echo $expiryErr;?></span>
            <br><br>
        </div>
        <hr>
        <label><b>Total Amount</b></label>
        <input  name="total" id="total" type="text" readonly>
        <br><br>
        <button type="submit" class="bookbtn" name="order" value='order' id='bookbtn' >Book Now!</button>
    </form>
</div>



可移动的

到期 $\u POST[“cust[XXX]”无效。如果必须使用2d数组来验证数据,则应改用$_POST['cust']['XXX']。下面的代码工作正常

if (empty($_POST["cust"]['name'])) {
    $nameErr = "Name is required"; 
} 
else {
    $name = validate($_POST["cust"]['name']);
    .............
}
使用表单发布的数据已序列化到名为$\u post的数组中。要在数组中获取数组,需要使用上面的语法。 如果您没有上述表单的多个实例,那么在表单输入中使用单个维度数组就可以了

<form action="" method="POST"> 
    <input type="text" name="name"></input>
    ......
</form>

......
然后您可以获得如下所示的post数据:

<?php
   if(empty($_POST['name'])){
       $nameErr = 'Please input your name';
   }else{
       //Do something to process the value submitted
   }
?>

您可以这样做:
对于html:
我需要检查所有验证是否都在会话中,然后重定向到收据页面。我已经做了验证。现在,如果所有输入都存在,我需要在会话中写入checl并发送到receive.php任务要求名称为cust[name]等