简体   繁体   English

使用“ $ _POST”命令将变量从表单馈送到PHP脚本时出现问题

[英]Trouble Feeding Variables from a Form into a PHP Script Using the “$_POST” Command

I am having some difficulty feeding variables from my html form into my php. 我在将变量从html表单输入到php中时遇到了一些困难。 Would someone mind helping me, I have been at this for hours. 有人介意帮助我,我已经在这里工作了几个小时。

HTML CODE: HTML代码:

<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Welcome to Chorelistings - Log in Here</title>

 <!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
 </head>
 <body>
 <form action="test.php" class="login">
<h1>Chore Listings </h1>
<input type = "email" id="email"  name = "email" class = "login-input" placeholder = "Username (Email)">   
<input type="password" name="password "  id ="password" class="login-input" placeholder="Password">
<input type="submit" value="Login" class="login-submit">
 <p class="create-account"><a href="register.html">Create Account</a></p>

<p class="login-help"><a href="index.html">Forgot password?</a></p>
</form>




 </body>
 </html>

My VERY SIMPLE php script: 我的非常简单的php脚本:

 <?php
    $email=$_POST['email']; 
     echo "hi";
  ?>

You have no method on your form, so it's defaulting to GET, which doesn't send $_POST values. 您的表单上没有method ,因此默认为GET,它不会发送$_POST值。

Add method="post" to your <form> tag. method="post"添加到您的<form>标记中。

A few things here. 这里有几件事。 Your form does not specify a request method. 您的表单未指定请求方法。 Add a method attribute to your form tag like so: method属性添加到表单标签中,如下所示:

<form method="post" action="test.php" class="login">

Next, just a side-note, there's no need for spaces when specifying your attributes, eg: 接下来,只是一个注释,在指定属性时不需要空格,例如:

<input type = "email" id="email"  name = "email" class = "login-input" placeholder = "Username (Email)" />

You'll be good to go with: 您将可以搭配使用:

<input type="email" id="email" name="email" class="login-input" placeholder="Username (Email)" />

A few (less common) things to think about -- make sure your .htaccess is not rewriting any post requests in an unwanted way. 需要考虑的一些(较不常见)的事情-确保您的.htaccess不会以不需要的方式重写任何发布请求。 If you happen to have any file uploads within your form(s), make sure you specify the enctype attribute on your form eg: 如果您碰巧在表单中上传了任何文件,请确保在表单上指定enctype属性,例如:

enctype="multipart/form-data"

Best of luck! 祝你好运!

Firstly, try adding method="post" to the <form> tag: 首先,尝试将method="post"添加到<form>标签:

<form action="test.php" class="login" method="post">

Secondly, inside your test.php file, try debugging what was actually sent: 其次,在您的test.php文件中,尝试调试实际发送的内容:

<?php
print_r($_POST);
// or
var_dump($_POST);
// or for more info
print_r($_REQUEST);
?>

Thirdly, try to remove whitespaces when defining your input fields in html: 第三,尝试在html中定义输入字段时删除空格:

<input type="email" id="email" name="email" class="login-input" placeholder="Username (Email)">

And finally, add <submit> before closing </form> to your form to actually post the data. 最后,在关闭</form> <submit>之前将<submit>添加到表单中以实际发布数据。 Currently, I see you have links ( <a href> ) but that won't submit the form. 目前,我看到您有链接( <a href> ),但是不会提交该表单。

So your final html should look (simplified) like that: 因此,最终的html应该看起来像(简化)一样:

<form method="post" action="test.php">
<input type="text" name="email">
<submit>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM