php 如何从文本字段中获取值并将其传递给变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5599857/
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 to get the value from a text field and pass it into a variable?
提问by Patrioticcow
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 变量中。我有以下代码。
<?php
$get_username = $_POST['username'];
print($get_username);
?>
thanks
谢谢
采纳答案by k to the z
Every time you hit the submit button, it will post the value of the input. This will set your PHP variable every time.
每次点击提交按钮时,它都会发布输入的值。这将每次设置您的 PHP 变量。
Update: Patrioticcow said his variable isn't returning anything. Something that should have been included in the question, but now we have that information.
更新:Patrioticcow 说他的变量没有返回任何东西。应该包含在问题中的内容,但现在我们有了这些信息。
echo $yourvariable;
Instead of print. This will work.
而不是印刷。这将起作用。
回答by Michael Berkowski
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.
除非您已将先前的值存储在$_SESSION
数组中,否则每次再次提交表单时您都将无法访问它。您$get_username
每次都会被覆盖。
回答by jihop
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.
我不太明白“在文本字段中输入另一个值”是什么意思,但是对于 HTTP,每个请求都与任何其他请求分开处理。因此,如果您再次提交,则会显示新值。
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:
如果您正在徘徊如何使用相同的键传递多个值以及如何检索这些值 sin php,这就是您要做的:
$usernames = array($_POST['username']); $username_one = $usernames[0]; $username_two = $usernames[1]; ...
$usernames = array($_POST['username']); $username_one = $usernames[0]; $username_two = $usernames[1]; ...