Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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_Forms_Validation - Fatal编程技术网

使用操作进行PHP错误验证

使用操作进行PHP错误验证,php,forms,validation,Php,Forms,Validation,我正在尝试创建一个简单的表单,如果用户没有输入邮政编码,它会在同一页面上给用户一个错误。如果他们输入了邮政编码,我希望表格转到另一页。有人能告诉我我做错了什么吗 <?php $errors = ""; if(isset($_GET['submit'])){ if(0 === preg_match("/\S+/", $_GET['zip'])) $errors['zip'] = '<li type="square" >To proceed please ent

我正在尝试创建一个简单的表单,如果用户没有输入邮政编码,它会在同一页面上给用户一个错误。如果他们输入了邮政编码,我希望表格转到另一页。有人能告诉我我做错了什么吗

<?php 

$errors = "";

if(isset($_GET['submit'])){

if(0 === preg_match("/\S+/", $_GET['zip']))   
   $errors['zip'] = '<li type="square" >To proceed please enter a valid ZIP Code.</li>'; 
} else {
    die(header("Location: success.php"));
}   

?>

<?php

if ($errors != "") {   
    echo $errors['zip']; 
} 

?>    




<form name="zipcodeform" id="zipcodeform" action="" method="GET">
    <br />
    <h3>Try your self :</h3><br />

    <table border="0" cellpadding="2" cellspacing="2">
        <tr>
            <td align="left" valign="top">Zip Code : </td>
            <td align="left" valign="top"><input type="text" name="zip" id="zip" value="<?php echo $_GET['zip']; ?>"  /></td>
        </tr>

        <tr>
            <td>&nbsp;</td>
            <td align="left" valign="top"><input type="submit" name="submit"   id="submit"      value="Get Quotes >> " /></td>
        </tr>


</form>

有很多东西你应该去寻找:

  • $errors
    是字符串还是数组?如果它是一个数组,您应该正确初始化它
  • 使用更严格的错误报告(E_ALL)。您将收到一条通知,在未提交表单的情况下,打开页面时未设置
    $\u get['plz']
  • 始终使用
    {}
    。这是一种很好的编码风格
  • 如果你考虑到所有这些,脚本应该运行良好,或者至少你应该看到你的错误。在我这里很好。

    试试这个

    假设您的第一个(form.html)页面是这样的

    <html>
    <head> ... </head>
    <body>
     <form method='get' action='proceed.php'>
       Pin code : <input type='text' name='pincode'>
     <input type='Submit'>
    </form>
    </body>
    </html>
    
    
    ... 
    Pin码:
    
    然后是处理表单的PHP页面(procedure.PHP)

    
    ... 
    
    脚本中有多个错误。试一试

    <?php 
    
    $errors = array();
    
    if(isset($_GET['submit'])){
      if( !preg_match("/\S+/", $_GET['zip']) ){
            $errors['zip'] = '<li type="square" >To proceed please enter a valid ZIP Code.</li>';
      } 
    
      if( !empty($errors) ){
          echo $errors['zip'];
      } else {
          header("Location: success.php");
          exit();
      }
    
    } 
    
    ?>
    
    <form name="zipcodeform" id="zipcodeform" action="" method="GET">
    <br />
    <h3>Try your self :</h3><br />
    
    <table border="0" cellpadding="2" cellspacing="2">
    <tr>
    <td align="left" valign="top">Zip Code : </td>
    <td align="left" valign="top"><input type="text" name="zip" id="zip" value="<?php if(isset($_GET['zip'])) echo $_GET['zip']; ?>"  /></td>
    </tr>
    
    <tr>
    <td>&nbsp;</td>
    <td align="left" valign="top"><input type="submit" name="submit"  id="submit"   value="Get Quotes &gt;&gt;" /></td>
    </tr>
    </table>
    
    </form>
    

    你能发布你的具体错误吗?检查
    if(0===preg\u match(“/\S+/”,$\u GET['zip'))
    条件是否通过简单地回显if和else块中的某些内容起作用。另外,请提供有关表单中发生的情况的更多详细信息。
    value=“Get Quotes>>”
    @Fred的内部值是否重要?它将显示为按钮文本,对吗?@user2608865很高兴帮助您!如果它有助于考虑接受答案:“事实上,不应该有任何<代码> >代码>。它应该读作
    value=“Get Quotes”
    ——是的,这很重要,因为在某些情况下,
    td
    @AnkitPokhrel中没有PHP速记代码,根据表单的显示方式,最好使用
    符号,而不是
    。我知道OP现在想做什么了。
    <?php 
    
    $errors = array();
    
    if(isset($_GET['submit'])){
      if( !preg_match("/\S+/", $_GET['zip']) ){
            $errors['zip'] = '<li type="square" >To proceed please enter a valid ZIP Code.</li>';
      } 
    
      if( !empty($errors) ){
          echo $errors['zip'];
      } else {
          header("Location: success.php");
          exit();
      }
    
    } 
    
    ?>
    
    <form name="zipcodeform" id="zipcodeform" action="" method="GET">
    <br />
    <h3>Try your self :</h3><br />
    
    <table border="0" cellpadding="2" cellspacing="2">
    <tr>
    <td align="left" valign="top">Zip Code : </td>
    <td align="left" valign="top"><input type="text" name="zip" id="zip" value="<?php if(isset($_GET['zip'])) echo $_GET['zip']; ?>"  /></td>
    </tr>
    
    <tr>
    <td>&nbsp;</td>
    <td align="left" valign="top"><input type="submit" name="submit"  id="submit"   value="Get Quotes &gt;&gt;" /></td>
    </tr>
    </table>
    
    </form>