为什么我的表单变量没有通过POST?

时间:2013-11-24 13:15:38

标签: php forms variables input set

我通过POST发送表单数据,但没有设置相应的POST变量,而不是。

此外,当我将POST数据存储到本地PHP变量时,我似乎无法使用这些变量。 (一旦我解决了第一个问题,我觉得我也可以使用变量。)

第二页输出的错误消息(见下文)是:

Notice: Undefined variable: postUsername in (...somepath)\scripts\create-member.php on line 10

(表格页面):

<form action="scripts/create-member.php" method="POST">

        <input type="text" name"username" value="" placeholder="User Name"> <br />
        <input type="password" name"password" value="" placeholder="Password"> <br />
        <input type="password" name"passwordConfirm" value="" placeholder="Confirm Password"> <br />
        <!-- ?type email or type text -->
        <input type="email" name"email" value="" placeholder="Email" autofocus> <br />
        <input type="submit" name="submitRegistration" value="Register!">

    </form>

(第二页)scripts \ create-member.php:

<?php

//!proper way to declare variables obtained from POST.
// Data from form "register.php"
if ( isset($_POST['username']) ) {
    $postUsername = $_POST['username'];
}


echo $postUsername;    // <-- this is line 10

&GT;

我也尝试过使用isset()作为提交按钮,但这并没有解决问题。 我在这里已经对代码进行了大量简化,并对其进行了测试。

3 个答案:

答案 0 :(得分:11)

在您的HTML代码中,=

错过了name
name="username"

而不是name"username"


这是您的固定代码。

<input type="text" name="username" value="" placeholder="User Name"> <br />
<input type="password" name="password" value="" placeholder="Password"> <br />
<input type="password" name="passwordConfirm" value="" placeholder="Confirm Password"> <br />
<!-- ?type email or type text -->
<input type="email" name="email" value="" placeholder="Email" autofocus> <br />

答案 1 :(得分:2)

ploblem不在register.php

您可以尝试这样写:

if ( isset($_POST['username']) ) {
$postUsername = $_POST['username'];
echo $postUsername;
}

答案 2 :(得分:0)

我在文件夹中的index.php文件中发布了类似的问题。

<form id="register" name="register" method="POST" action="/register">
// BROKEN

所有$ _POST变量都是空的,直到我在表单后期操作中添加了一个尾随正斜杠

<form id="register" name="register" method="POST" action="/register/">
// WORKS