2014-10-31 60 views
0

我想驗證我的形式,我得到這個錯誤注意:在PHP(驗證)未定義指數

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"]; 
       } 
      } 
?> 

我得到了錯誤僅此輸入而輸入的一切也適用於其他

回答

0

使用isset檢查,如果在$ _ POST鍵存在

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

你必須檢查isset()函數不爲NULL精細

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

但在那之後,它不會顯示錯誤代碼「輸入數量」? – 2014-10-31 04:31:08

+0

如果$ _POST ['ReqQty']沒有設置,請使用if(!isset($ _ POST ['ReqQty'])) – Akilan 2014-10-31 04:33:45

0

發生此錯誤的原因是該字段未出現在請求中,因此您沒有密鑰$_POST。爲了防止這種情況,您必須檢查密鑰是否存在,然後驗證它。

檢查一個更好的辦法是:

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

刪除disabled;如果你想要你可以使用隱藏。從您input field

A disabled input element is unusable and un-clickable.意味着你不能使用它們。他們沒有進一步發送價值

如果您想要我在我的答案中使用,您可以使用readonly tag。 (由​​建議。)

<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";}?>> 
+1

好得多,以便它可以只讀,以便它仍然具有與文本框相同的外觀。並且仍然包含在POST – Ghost 2014-10-31 04:33:01

+0

的內部,但是當輸入的其中一個插入到表單中的某個文本框中時,它將刪除禁用的attr並使用戶能夠輸入數據 – 2014-10-31 04:33:59

+0

@ Arif_suhail_123奇怪的是:當OP的屬性爲「disabled/readonly」時,爲什麼OP想要發生按鍵事件?沒有意義。 – Ghost 2014-10-31 04:38:07