我有一个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>
我希望此表单的用户在输入框中输入数据。然后我希望这个数据是PHP字符串的值,例如$username = "MY_NAME";
,其中MY_NAME
是用户输入的HTML表单的值。
如果用户在输入框中的输入是例如"STACKOVERFLOW"
,我希望PHP字符串是$username = "STACKOVERFLOW";
表单提交时,需要从$_POST
数组中获取值
您可以执行print_r($_POST)
来查看它包含的所有内容(所有表单字段),并单独引用它们。
用户名将为$_POST['username']
我建议阅读一篇关于使用表单和PHP的教程。。。这是一个好的
既然你显然是个初学者,我会多帮你一点:
为您的提交按钮命名:
<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>
由于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
首先检查表单是否已提交:
<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'];
}