我如何从HTML表单获取PHP值? [英] How can I get a PHP value from an HTML form?

查看:211
本文介绍了我如何从HTML表单获取PHP值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < form enctype =multipart / form-dataaction = method =post> 
< input name =usernametype =text/>
< input type =submitvalue =Uploadclass =btn btn-primary/>< br />
< / form>

我希望这个表单的用户在输入框中输入数据。然后,我希望这些数据是PHP字符串的值 - 例如 $ username =MY_NAME; 其中 MY_NAME 是用户输入的HTML表单的值。



如果用户在输入框中的输入是eg STACKOVERFLOW我希望PHP字符串为 $ username =STACKOVERFLOW;



预先感谢。

解决方案

当表单提交时,您需要从 $ _ POST array



您可以执行 print_r($ _ POST)来查看它包含的所有内容(所有表单域)和

用户名将会是 $ _ POST ['username']



我推荐阅读使用表单和PHP的教程... 这是一个很好的选择



既然你明显是初学者,我会帮你多一点:



为您的提交按钮命名:

 < form enctype =multipart / form-data动作=method =post> 
< input name =usernametype =text/>
< input type =submitname =submitvalue =Uploadclass =btn btn-primary/>< br />
< / form>

因为 action 为空,所以POST到当前页面。在你的文件的顶部,你可以通过检查是否设置了 $ _ POST ['submit'] '来检查表单是否被提交。 )。
$ b $ pre $ if(isset($ _ POST ['submit'])){
// form submitted,now我们可以查看通过
得到的数据//括号内的值来自输入字段的name属性。 (就像上面提交)
$ username = $ _POST ['username'];

//现在你可以对这个变量做任何事情。
}
//关闭PHP标记,您的HTML将低于


I have an HTML form as follows:

<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_NAME is the value of the HTML form entered by the user.

If the input by the user in the input box is e.g. "STACKOVERFLOW" I want the PHP string to be $username = "STACKOVERFLOW";

Thanks in advance.

解决方案

When the form submits, you need to get the values from the $_POST array

You can do print_r($_POST) to see everything it contains (all of the form fields) and reference them individually as well.

Username will be $_POST['username']

I recommend reading a tutorial on working with forms and PHP... here's a good one

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 action is 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).

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

这篇关于我如何从HTML表单获取PHP值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆