php - html只在表单的一个元素上形成$ _POST undefined index?

时间:2017-06-21 06:38:32

标签: php html forms


今天我遇到了一个非常令人沮丧的错误(即使这肯定是我的错):我有一个带有几个输入元素的html表单,将它们发布到php文件中。我继续得到未定义的索引错误,因为其中一个表单元素不想发布。这是我的html代码(简化)(/forms/form.php):

<form action="/php/save.php" method="post">
    <table>
        <tr>
            <td><input type="text" value="" name="SpeechmanWeek1"/></td>
            <td><input type="text" value="" name="SpeechmanWeek2"/></td>
            <td><input type="text" value="" name="SpeechmanWeek3"/></td>
            <td><input type="text" value="" name="SpeechmanWeek4"/></td>
        </tr> 
        <tr>
            <td><input type="text" value="" name="ReaderWeek1"/></td>
            <td><input type="text" value="" name="ReaderWeek2"/></td>
            <td><input type="text" value="" name="ReaderWeek3"/></td>
            <td><input type="text" value="" name="ReaderWeek4"/></td>
        </tr> 
        <tr>
            <td><input type="submit" value="Submit" name="submit"/></td>
            <td></td>
            <td></td>
            <td></td>
        </tr> 
    </table>
</form>

现在,这是我的PHP代码(简化)(/php/save.php):

<?php 
    if(isset($_POST['submit'])) {
        $SpeechmanWeek1 = $_POST['SpeechmanWeek1'];
        $SpeechmanWeek2 = $_POST['SpeechmanWeek2'];
        $SpeechmanWeek3 = $_POST['SpeechmanWeek3'];
        $SpeechmanWeek4 = $_POST['SpeechmanWeek4'];
        // the above code works fine (I get no undefined index error for it)
        // but the following code keeps on causing an undefined index notice and I don't get why 
        $ReaderWeek1 = $_POST['ReaderWeek1'];
        $ReaderWeek2 = $_POST['ReaderWeek2'];
        $ReaderWeek3 = $_POST['ReaderWeek3'];
        $ReaderWeek4 = $_POST['ReaderWeek4'];
    }
?>

我继续收到此错误:

Notice: Undefined Index: ReaderWeek1 in /var/www/html/php/save.php
Notice: Undefined Index: ReaderWeek2 in /var/www/html/php/save.php
Notice: Undefined Index: ReaderWeek3 in /var/www/html/php/save.php
Notice: Undefined Index: ReaderWeek4 in /var/www/html/php/save.php

我已经看过其他线程,但实际上没有一个符合我的情况。 您是否知道为什么我会收到此错误以及为什么特定表单元素不会被发布而其他所有元素都会发布?
提前感谢您花时间回答我。

2 个答案:

答案 0 :(得分:-1)

在使用它们之前,你必须检查你的$ _POST-Variables。

if($_POST['SpeechmanWeek1']){
 $SpeechmanWeek1 = $_POST['SpeechmanWeek1'];
}else{
 $SpeechmanWeek1 = 'whateveryouwant';
}

答案 1 :(得分:-2)

请删除isset行,为您提供类似这样的内容

<?php 

    $SpeechmanWeek1 = $_POST['SpeechmanWeek1'];
    $SpeechmanWeek2 = $_POST['SpeechmanWeek2'];
    $SpeechmanWeek3 = $_POST['SpeechmanWeek3'];
    $SpeechmanWeek4 = $_POST['SpeechmanWeek4'];
    $ReaderWeek1 = $_POST['ReaderWeek1'];
    $ReaderWeek2 = $_POST['ReaderWeek2'];
    $ReaderWeek3 = $_POST['ReaderWeek3'];
    $ReaderWeek4 = $_POST['ReaderWeek4'];

?>