php - 注意:表单上的未定义索引

时间:2017-02-12 19:41:05

标签: php html forms

我正在尝试使用html和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!";
            }  

有什么不对吗?

2 个答案:

答案 0 :(得分: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!";
            }  
}

快乐编码:)

答案 1 :(得分:0)

你可以先检查你的php文件中的表单提交

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

或更多检查添加更多帖子变量检入条件。当使用没有post值的$ _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!";
        } 
    } 

}

?>