【问题标题】:Why are my form variables not passing through POST?为什么我的表单变量不通过 POST?
【发布时间】:2013-11-24 13:15:38
【问题描述】:

我是通过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>

(第二页)脚本\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

?>

我也尝试过使用 isset() 作为提交按钮,但这并没有解决问题。 我在这里将代码简化了很多,并运行它来测试它。

【问题讨论】:

  • 提供完整的 HTML?
    开始标签在哪里?
  • 把你的回声放在if(isset()){}中
  • 表单标签在那里,但 stackoverflow 需要我插入另一个换行符才能显示。
  • 我尝试回显一个文字字符串。它实际上什么都没有。
  • 将= 放入name"password"、name"username"、name"passwordConfirm" 等...

标签: php forms variables input set


【解决方案1】:

在您的 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 />

【讨论】:

    【解决方案2】:

    问题不在 register.php 中

    你可以试试这样写:

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

    【讨论】:

      【解决方案3】:

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

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

      在我向表单发布操作添加尾部正斜杠之前,所有 $_POST 变量都是空的

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-25
        相关资源
        最近更新 更多