PHP:POST未定义的索引错误

时间:2016-03-29 03:11:00

标签: php

我知道有类似的问题,但没有一个在我的案例中有所帮助。

我的表格是这样写的:



    <div class = "Contact">
        <form action="formprocessor.php" method="post">
            <label>Name: </label>
            <input name="name" type="text" size="25" />

            <label>Course: </label>
            <input name="course" type="text" size="25" />

            <label>Book: </label>
            <input name="book" type="text" size="255" />

            <label>Price: </label>
            <input name="price" type="text" size="7" />

            <label>Email: </label>
            <input name="email" type="text" size="255" />

            <label>Phone #: </label>
            <input name="phone" type="text" size="12" />

            <input name="mySubmit" type="submit" value="Submit!" />
        </form>
    </div>
&#13;
&#13;
&#13;

来自formprocessor.php的相关PHP代码:

<?php
$name = $_POST["name"];
$course = $_POST["course"];
$book = $_POST["book"];
$price = $_POST["price"];
$email = $_POST["email"];
$phone = $_POST["phone"];

echo $name;
?>

单击表单上的sumbit按钮会给我这些错误:

Notice: Undefined index: name in /Users/Jose/PhpstormProjects/334 Final Project/formprocessor.php on line 3

Notice: Undefined index: course in /Users/Jose/PhpstormProjects/334 Final Project/formprocessor.php on line 4

Notice: Undefined index: book in /Users/Jose/PhpstormProjects/334 Final Project/formprocessor.php on line 5

Notice: Undefined index: price in /Users/Jose/PhpstormProjects/334 Final Project/formprocessor.php on line 6

Notice: Undefined index: email in /Users/Jose/PhpstormProjects/334 Final Project/formprocessor.php on line 7

Notice: Undefined index: phone in /Users/Jose/PhpstormProjects/334 Final Project/formprocessor.php on line 8

感谢任何帮助。提前致谢

2 个答案:

答案 0 :(得分:0)

<?php
if(isset($_POST['mySubmit']))
{
    $name = $_POST["name"];
    $course = $_POST["course"];
    $book = $_POST["book"];
    $price = $_POST["price"];
    $email = $_POST["email"];
    $phone = $_POST["phone"];

    echo $name;
}
?>

答案 1 :(得分:0)

<?php
$name   = isset($_POST["name"]) ? $_POST["name"] : '';
$course = isset($_POST["course"]) ? $_POST["course"] : '';
$book   = isset($_POST["book"]) ? $_POST["book"] : '';
$price  = isset($_POST["price"]) ? $_POST["price"] : '';
$email  = isset($_POST["email"]) ? $_POST["email"] : '';
$phone  = isset($_POST["phone"]) ? $_POST["phone"] : '';

echo $name;
?>