如何从 HTML 表单中获取 PHP 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11569971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get a PHP value from an HTML form?
提问by Piedpiper Malta
I have an HTML form as follows:
我有一个 HTML 表单,如下所示:
<form enctype="multipart/form-data" action=" " method="post">
<input name="username" type="text"/>
<input type="submit" value="Upload" class="btn btn-primary"/><br/>
</form>
and I want that the user of this form enters data in the input box. Then I would like this data to be the value of a PHP string - e.g. $username = "MY_NAME";where MY_NAMEis the value of the HTML form entered by the user.
我希望这个表单的用户在输入框中输入数据。然后我希望这个数据是一个 PHP 字符串的值- 例如$username = "MY_NAME";,MY_NAME用户输入的 HTML 表单的值在哪里。
If the input by the user in the input box is e.g. "STACKOVERFLOW"I want the PHP string to be $username = "STACKOVERFLOW";
如果用户在输入框中的输入是例如"STACKOVERFLOW"我希望 PHP 字符串是 $username = "STACKOVERFLOW";
Thanks in advance.
提前致谢。
回答by sachleen
When the form submits, you need to get the values from the $_POSTarray
当表单提交时,您需要从$_POST数组中获取值
You can do print_r($_POST)to see everything it contains (all of the form fields) and reference them individually as well.
您可以print_r($_POST)查看它包含的所有内容(所有表单字段)并单独引用它们。
Username will be $_POST['username']
用户名将是 $_POST['username']
I recommend reading a tutorial on working with forms and PHP... here's a good one
我建议阅读有关使用表单和 PHP 的教程......这是一个很好的教程
Since you're obviously a beginner I'll help you out a bit more:
由于您显然是初学者,因此我会为您提供更多帮助:
Give your submit button a name:
为您的提交按钮命名:
<form enctype="multipart/form-data" action="" method="post">
<input name="username" type="text"/>
<input type="submit" name="submit" value="Upload" class="btn btn-primary"/><br/>
</form>
Because actionis blank, it will POST to the current page. At the top of your file, you can check to see if the form was submitted by checking if $_POST['submit']is set (I gave your submit button that name).
因为action是空白,它会POST到当前页面。在文件的顶部,您可以通过检查是否$_POST['submit']设置(我给您的提交按钮命名)来检查表单是否已提交。
if(isset($_POST['submit'])) {
// form submitted, now we can look at the data that came through
// the value inside the brackets comes from the name attribute of the input field. (just like submit above)
$username = $_POST['username'];
// Now you can do whatever with this variable.
}
// close the PHP tag and your HTML will be below
回答by brezanac
Some basic reading on handling forms in PHP.
关于在 PHP 中处理表单的一些基本阅读。
回答by ewein
First check if the form has been submitted:
首先检查表单是否已经提交:
<form enctype="multipart/form-data" action=" " method="post">
<input name="username" type="text"/>
<input type="submit" name="Submit" value="Upload" class="btn btn-primary"/><br/>
</form>
if($_POST['Submit'] == "Upload")
{
$username = $_POST['username'];
}

