为什么我的POST是一个未定义的数组?

时间:2015-06-03 14:23:35

标签: php html post

我一直在制作各种各样的信息,我最近遇到了这个问题:

每次我尝试从HTML表单中获取特定的POST值时,都会收到如下错误:
 注意:未定义的索引:第15行的C:\ xampp2 \ htdocs \ weekAttendance.php中的lastN

现在,我已经花了三天时间尝试谷歌/堆叠答案,我已经尝试了几乎所有的解决方案,例如使用isset,mysqli_real_escape_string,检查以确保我的HTML表单具有正确的名称属性,以及更多但是我仍然不确定为什么我收到这个错误。我有三个其他帖子,按照我使用的方法工作得很好。任何人都可以帮助我吗?

这是我的相关HTML:

$(".col_1_6").each(function(index) {
    $(this).next(".col_1_4").andSelf().wrapAll("<div class='row' />")
});

这是weekAttendance.php:

<form action="weekAttendance.php" method="POST">
Select Student Name: 
<select id="lastN" name="lastN">
<option value="" selected> Select a Name from the List Below</option>
<option value="Alvarez">Liana Alvarez</option>
<option value="Barlow">Debra Barlow</option>
<option value="Bester">Anthony Bester</option>
<option value="Delvalle">Madelyn Delvalle</option>
<option value="Flowers">Crystal Flowers</option>
<option value="Hoefner">Peggy Hoefner</option>
<option value="Mingo">Kamika Mingo</option>
<option value="Richards">Deyanira Richards</option>
<option value="Sims">Charles Sims</option>
<option value="Wixom">Jason Wixom</option>
<option value="Pittman">Toniqua Pittman </option>
<option value="Mainer">Sandra Mainer</option>
</select>
<br>
Date: <input type="date" name="daydate">
<br>
Time In: <input type="time" name="inD">
Time Out:<input type="time" name="outD">
<br>
Not listed here? <a href="#">Click here to create a new Student.</a>
<br>
<input type="submit">
</form>

2 个答案:

答案 0 :(得分:2)

这一行:

$lastName=if(isset($_POST['lastN'])){ $lastName=$_POST['lastN']; };
正如我在评论中所说,这是不正确的。

用以下内容替换现有的块:

$dDate=date("l", (strtotime($_POST['daydate'])));
$mon;
$tue;
$wed;
$thur;
$fri;
$dayVar;
$timeIN=$_POST['inD'];
$timeOUT=$_POST['outD'];

if(isset($_POST['lastN'])){
$lastName=$_POST['lastN'];

echo $lastName;

}

$hoursDay;
$weekID;

// etc.

您也可以尝试将isset替换为!empty

您也可以使用三元运算符:

$lastName=!empty($_POST['lastN']) ? $_POST['lastN'] : '';

重写:

$dDate=date("l", (strtotime($_POST['daydate'])));
$mon;
$tue;
$wed;
$thur;
$fri;
$dayVar;
$timeIN=$_POST['inD'];
$timeOUT=$_POST['outD'];

echo $lastName=!empty($_POST['lastN']) ? $_POST['lastN'] : '';

$hoursDay;
$weekID;

// etc.

您也可以将!empty替换为isset

修改

您收到该警告是因为echo $lastName;在条件语句之外。将它放在$lastName=$_POST['lastN'];之后的条件语句中。

编辑#2: - 测试示例:

我成功地测试了以下内容,所以我不知道你为什么会收到警告。

<form action="" method="POST">
Select Student Name: 
<select id="lastN" name="lastN">
<option value="" selected> Select a Name from the List Below</option>
<option value="Alvarez">Liana Alvarez</option>
<option value="Barlow">Debra Barlow</option>
<option value="Bester">Anthony Bester</option>
<option value="Delvalle">Madelyn Delvalle</option>
<option value="Flowers">Crystal Flowers</option>
<option value="Hoefner">Peggy Hoefner</option>
<option value="Mingo">Kamika Mingo</option>
<option value="Richards">Deyanira Richards</option>
<option value="Sims">Charles Sims</option>
<option value="Wixom">Jason Wixom</option>
<option value="Pittman">Toniqua Pittman </option>
<option value="Mainer">Sandra Mainer</option>
</select>
<br>
Date: <input type="date" name="daydate">
<br>
Time In: <input type="time" name="inD">
Time Out:<input type="time" name="outD">
<br>
Not listed here? <a href="#">Click here to create a new Student.</a>
<br>
<input type="submit">
</form>

<?php

if(isset($_POST['lastN'])){
$lastName=$_POST['lastN'];

    echo $lastName;

}

?>
  • 然后你需要填写剩下的部分。

编辑#3 (最后努力)

在此处,将提交按钮命名为:

<input type="submit" name="submit" value="SEND DATA">

然后执行:(并且是我最后的努力),完全重写并在同一个文件中运行,而不是单独的文件。

旁注:您也可以将action="weekAttendance.php"更改为action="",因为它位于同一文件中。

<?php

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

?>

<form action="weekAttendance.php" method="POST">
Select Student Name: 
<select id="lastN" name="lastN">
<option value="" selected> Select a Name from the List Below</option>
<option value="Alvarez">Liana Alvarez</option>
<option value="Barlow">Debra Barlow</option>
<option value="Bester">Anthony Bester</option>
<option value="Delvalle">Madelyn Delvalle</option>
<option value="Flowers">Crystal Flowers</option>
<option value="Hoefner">Peggy Hoefner</option>
<option value="Mingo">Kamika Mingo</option>
<option value="Richards">Deyanira Richards</option>
<option value="Sims">Charles Sims</option>
<option value="Wixom">Jason Wixom</option>
<option value="Pittman">Toniqua Pittman </option>
<option value="Mainer">Sandra Mainer</option>
</select>
<br>
Date: <input type="date" name="daydate">
<br>
Time In: <input type="time" name="inD">
Time Out:<input type="time" name="outD">
<br>
Not listed here? <a href="#">Click here to create a new Student.</a>
<br>
<input type="submit" name="submit" value="SEND DATA">
</form>

<?php

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

    $dDate=date("l", (strtotime($_POST['daydate'])));
    $mon;
    $tue;
    $wed;
    $thur;
    $fri;
    $dayVar;
    $timeIN=$_POST['inD'];
    $timeOUT=$_POST['outD'];

        if(isset($_POST['lastN'])){
    $lastName=$_POST['lastN'];
    echo $lastName;
    }
    $hoursDay;
    $weekID;
    #$totalHours2= ($mon + $tue + $wed + $thur +$fri)/24)
    if ($dDate==="Monday"){
        $mon="Monday";
        $dayVar=$mon;
    }
    if ($dDate==="Tuesday"){
        $tue="Tue";
        $dayVar=$tue;   
    }
    if ($dDate==="Wednesday"){
        $wed="Wed";
        $dayVar=$wed;
    }
    if ($dDate==="Thursday"){
        $thur="Thurs";
        $dayVar=$thur;

    }
    if ($dDate==="Friday"){
        $fri="Fri";
        $dayVar=$fri;
    }

    /*$sql="INSERT INTO $dayVar ('$dayVar', first_name2, last_name2,wid)
    VALUES($hoursDay','$firstName', '$lastName','$weekId');*/
    echo $dayVar." <strong> Time In</strong> " .$timeIN." <strong>Time Out</strong> " .$timeOUT;
    echo $lastName;

} // brace for if(isset($_POST['submit']))

?>

答案 1 :(得分:0)

由于您使用的是程序化PHP,因此最好将表单提交放在isset()函数中,同时为<input type="submit">提供名称属性,可能是“name='submit'”。 所以你会有

if(isset($_POST['submit'])){
//do your code here
}

另外请确保没有其他具有相同名称的表单字段。 最后一件事,但并非最不重要: 你做切换案例比使用多个if语句更好。

$day = $_POST['day'];

switch($day){
case: "Monday"
$day = "Mon";
break;
//Continue with other days here

}