doumeng3345
2017-02-12 19:41
浏览 68
已采纳

php - 注意:表单上的未定义索引[重复]

I'm trying to create a form using html and php and I get these error messages. Everything are in one php file. I can find anything that is wrong with my code.

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 code:-

<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 code where I get the error:-

//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!";
            }  

Is anything wrong?

</div>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douba4275 2017-02-12 19:51
    已采纳

    When you are running this PHP application for first time there is nothing in $_POST[] that is why it gives an error like this.(Because we have to submit the form to have values in $_POST[] array) you have to define it like this with isset() function

    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!";
                }  
    }
    

    Happy Coding :)

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doushaju4901 2017-02-12 19:46

    you can check first it is form submit in your php file first

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

    or more check add more post variable check in if condition . when use $_POST condition without post value then this warning will show

    so this is your final code

    <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>
    

    and php code

    <?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!";
            } 
        } 
    
    }
    
    ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题