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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀