如何在 PHP 中获取同一页面上文本框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17391149/
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 of a textbox on the same page in PHP
提问by Maryam Butt
Here is my code:
这是我的代码:
<form action="" method="get" >
<input type="text" name="un">
<input type="password" name="un2" />
<input type="submit" value="submit" name="submit" />
</form>
<?php
$users1 = $_GET["un"];
$id = $_GET["un2"];
echo $users1;
?>
I am unable to display it through this way
我无法通过这种方式显示它
error:
错误:
Notice: Undefined index: un in C:\wamp\www\vas1\register1.php on line 31
line 31:
第 31 行:
$users1 = $_GET["un"];
回答by SeanWM
It's just a notice. You need to check if the form is being submitted:
这只是一个通知。您需要检查表单是否正在提交:
if(!empty($_POST)) {
$users1 = $_POST['un'];
echo $users1;
}
You can't be using using get
because your form is using post
:
您不能使用 usingget
因为您的表单正在使用post
:
<form action="" method="post">
回答by hellosheikh
you are sending the post request
您正在发送发布请求
<form action="" method="post" >
and then you are getting the parameter in get request
然后你在获取请求中获取参数
$users1 = $_GET["un"];
you are doing wrong ..do this
你做错了..这样做
$users = $_POST["un"];
回答by chrislondon
You have a couple issues in your code.
您的代码中有几个问题。
Firstly, on the first page load the textarea hasn't been submitted so the request data will be blank. You'll need a isset()
to test for it.
首先,在第一页加载时,textarea 尚未提交,因此请求数据将为空白。你需要一个isset()
来测试它。
Secondly, your PHP is using $_GET
when your form is using POST
so you need to change those.
其次,$_GET
当您的表单正在使用时,您的 PHP 正在使用,POST
因此您需要更改它们。
Putting it all together:
把它们放在一起:
<form action="" method="post" >
<input type="text" name="un">
<input type="password" name="un2" />
<input type="submit" value="submit" name="submit" />
</form>
<?php
if (isset($_POST['un'])) {
$users1 = $_POST["un"];
$id = $_POST["un2"];
echo $users1;
}
?>