如何从文本字段获取值并将其传递给变量?
我有以下表格。
<form name="cookieform" id="login" method="POST">
<input type="text" NAME="username" id="username" class="text" maxlength="30" />
</form>
我想做的是从文本字段中获取值并将其放入 PHP 变量中。我有以下代码。
<?php
$get_username = $_POST['username'];
print($get_username);
?>
谢谢
I have the following form.
<form name="cookieform" id="login" method="POST">
<input type="text" NAME="username" id="username" class="text" maxlength="30" />
</form>
What I would like to do is to grab the value from the text field and place it into a PHP variable. I have the following code.
<?php
$get_username = $_POST['username'];
print($get_username);
?>
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次点击提交按钮时,它都会发布输入的值。这将每次设置您的 PHP 变量。
更新:
Patrioticcow 说他的变量没有返回任何内容。一些本来应该包含在问题中的内容,但现在我们有了这些信息。
而不是打印。这会起作用。
Every time you hit the submit button, it will post the value of the input. This will set your PHP variable every time.
Update:
Patrioticcow said his variable isn't returning anything. Something that should have been included in the question, but now we have that information.
Instead of print. This will work.
除非您已将之前的值存储在
$_SESSION
数组中,否则每次再次提交表单时您将无法访问它。您的$get_username
每次都会被覆盖。Unless you have stored your previous value in the
$_SESSION
array, you will lose access to it each time the form is submitted again. Your$get_username
will be overwritten each time.我不太明白“在文本字段中输入另一个值”是什么意思,但是使用 HTTP,每个请求都与任何其他请求分开处理。因此,如果您再次提交,则会显示新值。
如果您正在思考如何使用相同的键传递多个值以及如何在 php 中检索这些值,那么您可以这样做:
$usernames = array($_POST['username']);
$username_one = $usernames[0];
$username_two = $usernames[1];
...
I don't quite understand what you mean by "enter another value in the text field" but with HTTP, every request is handled separate from any other request. So if you submit again, the new value will show.
If you are wandering how to pass multiple values with the same key and how to retrieve those value sin php, this is what you do:
$usernames = array($_POST['username']);
$username_one = $usernames[0];
$username_two = $usernames[1];
...