【问题标题】:Notice: Undefined index in php (validation)注意:php中未定义的索引(验证)
【发布时间】:2014-10-31 04:26:30
【问题描述】:

我想验证我的表单并收到此错误

Notice: Undefined index: ReqQty in C:\project\user\requisition.php on line 180

这是我的代码:

    <td><label for="ReqQty">Quantity </label></td>
    <td colspan="3"><input name="ReqQty" id="ReqQty" onkeypress="return numbersOnly(event)" onkeyup="ItmQty_Availability()" disabled="disabled">

<?php
          $strReqQty = "";
          if(!empty($_POST)){
              if($_POST["ReqQty"]==NULL){ //ERROR here
                  echo "<font color=red>Enter the Quantity</font>";
              }else{
                  $strReqQty = $_POST["ReqQty"];
              }
          }
?>

我只收到此输入类型的错误,而其他输入类型一切正常

【问题讨论】:

    标签: php html sql-server validation


    【解决方案1】:

    删除disabled;如果你愿意,你可以使用隐藏。来自您的input field

    A disabled input element is unusable and un-clickable. 表示您不能使用它们。他们没有进一步发送任何价值

    如果您愿意,可以使用readonly tag,就像我在回答中使用的那样。 (由@ghost建议。)

    <td colspan="3"><input name="ReqQty" id="ReqQty" onkeypress="return numbersOnly(event)" onkeyup="ItmQty_Availability()" readonly>
    

    检查一下

    <td colspan="3"><input name="ReqQty" id="ReqQty" onkeypress="return numbersOnly(event)" onkeyup="ItmQty_Availability()" <?php if($condition==TRUE){echo "disabled=disabled";}?>>
    

    【讨论】:

    • 最好将其设置为只读,这样它的外观仍与文本框相同。并且仍然包含在 POST 中
    • 但这是针对将其中一个输入插入表单中的文本框之一的事件,它将删除禁用的属性并使用户能够输入数据
    • @Arif_suhail_123 奇怪的是:当 OP 的属性为 disabled/readonly 时,为什么要进行按键事件。没有意义。
    • onkeypress 事件是为了让用户插入的数据只有数字。当用户从 Autocomplete jQuery 中选择数据时,disabled 与 jQuery 事件有关,它将启用 ReqQty 字段被插入。否则它将保持禁用状态
    • 你能解释一下 if($condition==TRUE) 还有一个错误吗
    【解决方案2】:

    使用 isset 检查 $_POST 中的键是否存在

    <?php
          $strReqQty = "";
          if(!empty($_POST)){
              if(isset($_POST["ReqQty"])){
                  $strReqQty = $_POST["ReqQty"];
              }else{
                   echo "<font color=red>Enter the Quantity</font>";
              }
          }
    ?>
    

    【讨论】:

      【解决方案3】:

      你必须检查 isset() 不是 NULL

      if(isset($_POST["ReqQty"]) && $_POST["ReqQty"]=="" )
      

      【讨论】:

      • 但是之后就不会显示错误码“输入数量”了?
      • 如果 $_POST['ReqQty'] 没有设置,使用 if(!isset($_POST['ReqQty']))
      【解决方案4】:

      发生此错误是因为请求中不存在该字段,因此您在$_POST 上没有ReqQty 键。为防止这种情况发生,您必须检查密钥是否存在,然后对其进行验证。

      更好的检查方法是:

      if (!isset($_POST["ReqQty"]) || !$_POST["ReqQty"]) {
          // error
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-06
        • 1970-01-01
        • 1970-01-01
        • 2017-02-02
        • 2013-03-20
        • 2011-05-26
        • 2016-04-28
        • 1970-01-01
        相关资源
        最近更新 更多