问题:
表单提交后未定义的 POST 变量。
完成的研究和故障排除:
- 阅读这里的大量问题,几乎所有问题都与表单字段上没有姓名标签有关。我的所有字段都有标签和 ID。
- 将我的 PHP.ini 配置为将 $HTTP_RAW_POST_DATA 设置为 -1
- 学习了 PHP.net 和 W3SChools 的教程
此时我迷路了。数据只是拒绝发布,一切都未定义。下面是显示该问题的 HTML、PHP 和两个屏幕截图。
我在 Windows 上使用 PHPStorm 的内置服务器。
signup.html
<div class="text-center col-md-4 col-md-offset-4">
<form id="user_signup" class="form-horizontal signInFields" action="../php/register.php" method="POST">
<input type="text" id="first_name" name="first_name" placeholder="First Name">
<input type="text" id="last_name" name="last_name" placeholder="Last Name">
<input type="email" id="user_email" name="user_email" placeholder="Email">
<input type="text" id="user_id" name="user_id" placeholder="User ID">
<input type="password" id="user_password" name="user_password" placeholder="Password">
<input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
<button id="btn_signup" type="submit" name="signup_button">Sign Me Up!</button>
</form>
register.php
// Variables from the sign-up form POST action
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$user_email = $_POST["user_email"];
$user_id = $_POST["user_id"];
$user_password = $_POST["user_password"];
$confirm_password = $_POST["confirm_password"];
// Add a new user to the database
$conn = new mysqli($servername, $username, $password);
$testQuery = mysql_insert($first_name,$last_name,$user_email,$user_id,$user_password);
if($conn->query($testQuery) === TRUE){
echo "New Record Created Successfully!";
} else {
echo "Error: " . $testQuery . "<br>" . $conn->error;
}
$conn->close();
此时我感到困惑。据我从 W3Schools、PHP.net 和这里的各种问题中得知,我已经正确设置了它。然而,有些事情显然不对劲。我们将不胜感激任何帮助。
最佳答案
编辑:
您正在使用 PHPStorm 的 built-in web server ,现在有一些问题,尤其是 POST 请求,例如WEB-17317 .也可以引用这个answer在这方面。
这可能是您无法处理表单的原因。
因此,最好的解决方案是将您的 PHPStorm 配置为使用您的本地 Web 服务器,在您的情况下为 XAMPP,用于您当前的项目。按照以下步骤进行操作:
- 使用当前项目目录作为根目录配置本地服务器的虚拟主机
- 确保在上述步骤中配置后可以访问项目文件。例如:
http://localhost/signup.html
应该可以通过浏览器访问。 - 现在,在 PHPStorm 中,转到运行 -> 编辑配置 并选择每个文件并为每个文件配置 url,如下图 所示。例如。 signup.html 文件使用
http://localhost/signup.html
.- 您不需要为项目中的所有文件设置 url(第 3 步)。只需为 default(起始)文件执行此操作 - 例如index.html 或 login.html 或 signup.html。与
<a>
链接的所有其他文件或action="register.php"
表单中的内容将自动由本地服务器提供。
- 您不需要为项目中的所有文件设置 url(第 3 步)。只需为 default(起始)文件执行此操作 - 例如index.html 或 login.html 或 signup.html。与
现在,您可以使用 PHPStorm 中的绿色按钮运行您的 HTML/PHP 文件,它会在您的浏览器中使用您提供的 url 打开这些文件在步骤 1 中配置。您应该能够毫无问题地处理您的表单。
但如果您的问题是由其他原因引起的,请考虑检查以下几点:
- 表单字段应具有名称 属性,例如signup.html 文件中的“name='first_name”`。
-
echo $_SERVER["REQUEST_METHOD"] == "POST"
版画1
当写入 register.php 文件时,这意味着您的表单已成功提交。 - 值
post_max_size
在您的 php.ini 中设置为 #M 格式,例如10M
, 任何其他格式,如10MB
是无效的。引用php.net . - 检查是否
echo file_get_contents("php://input");
以以下格式显示数据:first_name=John&last_name=Doe ....
.引用php.net . - 检查是否
var_dump($_POST)
在数组中显示表单数据。
希望对你有帮助。欢迎发表评论。
关于php - PHP 中 POST 方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37506668/