Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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,Don';如果有错误,请不要提交_Php - Fatal编程技术网

PHP,Don';如果有错误,请不要提交

PHP,Don';如果有错误,请不要提交,php,Php,下面是我的PHP代码,目前,它显示了所有错误等,但如果其中一个是正确的,它将提交表单,我如何更改我的代码,以便在1不正确时不提交 <?php $cusMsg = ""; $fNameMsg = ""; if (isset($_POST["submit"])) { $id = $_POST["custid"]; if(empty($id)) { $cusMsg = '<span class="error"> Field was left e

下面是我的PHP代码,目前,它显示了所有错误等,但如果其中一个是正确的,它将提交表单,我如何更改我的代码,以便在1不正确时不提交

<?php
$cusMsg = "";
$fNameMsg = "";

if (isset($_POST["submit"])) {  
    $id = $_POST["custid"];

    if(empty($id)) {
        $cusMsg = '<span class="error"> Field was left empty</span>';
    } else if(!is_numeric($id)) {
        $cusMsg = '<span class="error"> Customer ID must be numeric</span>';
    } else if(strlen($id) != 6) {
        $cusMsg = '<span class="error"> Customer ID must be 6 digits long</span>';
    } else {
        return true;
    }

}

if (isset($_POST["submit"])) {  
    $fName = $_POST["customerfname"];
    $pattern = "/^[a-zA-Z-]+$/";

    if(empty($fName)) {
        $fNameMsg = '<span class="error"> Field was left empty</span>';
    } else if(!preg_match($pattern, $fName)) {
        $fNameMsg = '<span class="error"> First name must only containt letters and hyphens</span>';
    } else if(strlen($fName) > 20) {
        $fNameMsg = '<span class="error"> First name must not be longer than 20 characters</span>';
    } else {
        return true;
    }

}

}
?>

而不是上次使用else if并传递此

else if(!empty($fName) && preg_match($pattern, $fName) && strlen($fName) < 20){
return true;
}
else如果(!empty($fName)和&preg_match($pattern,$fName)和&strlen($fName)<20){
返回true;
}

它只是使用AND运算符检查所有条件,仅当满足所有条件时才返回true,而不是上次使用else if并通过此操作时返回else

else if(!empty($fName) && preg_match($pattern, $fName) && strlen($fName) < 20){
return true;
}
else如果(!empty($fName)和&preg_match($pattern,$fName)和&strlen($fName)<20){
返回true;
}

它只是使用AND运算符检查您的所有条件,并仅在满足所有条件时返回true

您可以在默认情况下将标志变量$submit设置为false

if (isset($_POST["submit"])) {  

   $submit = false; // Add this

   $id = $_POST["custid"];

   if (empty($id)) {

       $cusMsg = '<span class="error"> Field was left empty</span>';

   } else if (!is_numeric($id)) {

       $cusMsg = '<span class="error"> Customer ID must be numeric</span>';

   } else if(strlen($id) != 6) {

        $cusMsg = '<span class="error"> Customer ID must be 6 digits long</span>';

   } else {

      $submit = true;
   }

  // Now check the value of $submit and write your code accordingly.

  if ($submit) {
     // Write your submit action
  } else {
     // Other action
  }

}
if(isset($_POST[“submit”]){
$submit=false;//添加此
$id=$_POST[“custid”];
if(空($id)){
$cusMsg='字段为空';
}如果(!是数字($id))则为else{
$CUSMG='客户ID必须是数字';
}否则如果(strlen($id)!=6){
$CUSMG='客户ID必须为6位长度';
}否则{
$submit=true;
}
//现在检查$submit的值,并相应地编写代码。
如果($提交){
//写下你的提交动作
}否则{
//其他行动
}
}

您可以将标志变量$submit默认设置为false

if (isset($_POST["submit"])) {  

   $submit = false; // Add this

   $id = $_POST["custid"];

   if (empty($id)) {

       $cusMsg = '<span class="error"> Field was left empty</span>';

   } else if (!is_numeric($id)) {

       $cusMsg = '<span class="error"> Customer ID must be numeric</span>';

   } else if(strlen($id) != 6) {

        $cusMsg = '<span class="error"> Customer ID must be 6 digits long</span>';

   } else {

      $submit = true;
   }

  // Now check the value of $submit and write your code accordingly.

  if ($submit) {
     // Write your submit action
  } else {
     // Other action
  }

}
if(isset($_POST[“submit”]){
$submit=false;//添加此
$id=$_POST[“custid”];
if(空($id)){
$cusMsg='字段为空';
}如果(!是数字($id))则为else{
$CUSMG='客户ID必须是数字';
}否则如果(strlen($id)!=6){
$CUSMG='客户ID必须为6位长度';
}否则{
$submit=true;
}
//现在检查$submit的值,并相应地编写代码。
如果($提交){
//写下你的提交动作
}否则{
//其他行动
}
}

阅读手册中的内容
返回
does@Dagon谢谢,:)仔细阅读它,知道它是做什么的,去掉所有这些语句,然后将所有代码放入1 ISEST($_POST[“submit”]),它成功了:)去阅读手册中的内容
return
does@Dagon谢谢,:)仔细读一下,知道它做了什么去掉了所有语句,然后将所有代码放入1个ISEST($_POST[“submit”]),它成功了:)