2017-02-12 74 views
0

我正嘗試使用html和php創建表單,並且我收到了這些錯誤消息。一切都在一個PHP文件。我可以找到任何與我的代碼有關的錯誤。php - 注意:表單上的未定義索引

Notice: Undefined index: number in C:\xampp\htdocs\sms\textmessage.php on line 41 
Notice: Undefined index: sender in C:\xampp\htdocs\sms\textmessage.php on line 42 
Notice: Undefined index: message in C:\xampp\htdocs\sms\textmessage.php on line 43 
Notice: Undefined index: send in C:\xampp\htdocs\sms\textmessage.php on line 51 

表單代碼: -

<form method="POST"> 

    <div class="form-group"> 
     <label for="number">Receiver's Number</label> 
     <input type="tel" name="number" class="form-control" placeholder="Mobile Number" value="<?php echo $number; ?>" /> 
    </div> 

    <div class="form-group"> 
     <label for="sender">Sender</label> 
     <input type="text" name="sender" class="form-control" placeholder="Sender's Name" value="<?php echo $from; ?>"> 
    </div> 

    <div class="form-group"> 
     <label for="message">Message</label> 
     <textarea class="form-control" name="message" placeholder="Enter your message here" value="<?php echo $message; ?>"></textarea> 
    </div> 

     <input type="submit" class="btn btn-success btn-lg" id="submitbtn" value="Send" name="send"/> 
    <input type="hidden" name="submitted" value="true"> 

</form> 

PHP代碼,我得到的錯誤: -

//Get form data 
     $number = $_POST['number']; //Line 41 
     $from = $_POST['sender']; //Line 42 
     $message = $_POST['message']; //Line 43 
     $message = urlencode($message); //Test that the message is encoded correctly. Php will use urlencode the message 

     if ($_POST['send']) //Line 51 
     { 
      //Validating if the fields are empty 
      if ($number) 
      { 
       $error.="<br />Please enter the receiver's number!"; 
      } 
      if ($from) 
      { 
       $error.="<br />Please enter the Sender's name!"; 
      } 
      if ($message) 
      { 
       $error.="<br />Please enter the text message you want to send!"; 
      } 

有什麼不對?

+0

看到'<形式方法= 「POST」>'建議你用你的整個代碼在同一個頁面; HTML和PHP。錯誤/通知在這裏很明顯。當'

'沒有動作時,它默認爲「自我」,「在同一頁面/文件上」。你在下面有兩個很好的答案,接受其中的一個來解決這個問題。 –

回答

1

當您運行的是首次在PHP應用程序在$_POST[]中沒有任何內容,這就是爲什麼它會給出這樣的錯誤(因爲我們必須提交表單以在$_POST[]數組中有值),您必須使用isset()函數

if(isset($_POST['send'])){ 

    $number = $_POST['number']; //Line 41 
    $from = $_POST['sender']; //Line 42 
    $message = $_POST['message']; //Line 43 
    $message = urlencode($message); 

      //Validating if the fields are empty 
      if ($number) 
      { 
       $error.="<br />Please enter the receiver's number!"; 
      } 
      if ($from) 
      { 
       $error.="<br />Please enter the Sender's name!"; 
      } 
      if ($message) 
      { 
       $error.="<br />Please enter the text message you want to send!"; 
      } 
} 

編碼快樂:)

0

你可以先檢查一下它的形式在你的PHP文件中的第一

if(isset($_POST['submitted'])){ 
// then your code here 
} 

提交以上入住,如果條件添加更多的變量後檢查。當沒有崗位價值,可以用$ _ POST條件那麼,該警報將顯示

所以這是你的最終代碼

<form method="POST"> 

        <div class="form-group"> 
         <label for="number">Receiver's Number</label> 
         <input type="tel" name="number" class="form-control" placeholder="Mobile Number" value="<?php echo $number; ?>" /> 
        </div> 

        <div class="form-group"> 
         <label for="sender">Sender</label> 
         <input type="text" name="sender" class="form-control" placeholder="Sender's Name" value="<?php echo $from; ?>"> 
        </div> 

        <div class="form-group"> 
         <label for="message">Message</label> 
         <textarea class="form-control" name="message" placeholder="Enter your message here" value="<?php echo $message; ?>"></textarea> 
        </div> 

         <input type="submit" class="btn btn-success btn-lg" id="submitbtn" value="Send" name="send"/> 
        <input type="hidden" name="submitted" value="true"> 



       </form> 

和PHP代碼

<?php 
    if(isset($_POST['submitted'])){ 
    $number = $_POST['number']; //Line 41 
    $from = $_POST['sender']; //Line 42 
    $message = $_POST['message']; //Line 43 
    $message = urlencode($message); //Test that the message is encoded correctly. Php will use urlencode the message 

    if ($_POST['send']) //Line 51 
    { 
     //Validating if the fields are empty 
     if ($number) 
     { 
      $error.="<br />Please enter the receiver's number!"; 
     } 
     if ($from) 
     { 
      $error.="<br />Please enter the Sender's name!"; 
     } 
     if ($message) 
     { 
      $error.="<br />Please enter the text message you want to send!"; 
     } 
    } 

} 

?>