I can't seem to apply the $_POST
function properly to retrieve the data from a simple HTML form. I'm new to PHP, so I may be overlooking something simple.
我似乎无法正确应用$ _POST函数从简单的HTML表单中检索数据。我是PHP的新手,所以我可能会忽略一些简单的东西。
I have a form with action directing to the same page, but if the form is filled out, the value of $_POST['input_name']
will have changed, so I can trigger the php function using that condition. Is this the best way to structure this kind of action?
我有一个带有动作的表单指向同一页面,但是如果填写了表单,$ _POST ['input_name']的值将会改变,所以我可以使用该条件触发php函数。这是构建这种行为的最佳方式吗?
Here's my HTML code (thispage.php is the current/same page):
这是我的HTML代码(thispage.php是当前/同一页面):
<form action="thispage.php" method="post">
Name: <input type="text" name="userName" id="userName" />
<input type="submit" />
</form>
Here's my PHP code from the same page:
这是我在同一页面上的PHP代码:
if("" == trim($_POST['userName'])){
echo $_POST['userName'];
}
Thanks a lot in advance!!
非常感谢提前!!
4 个解决方案
#1
4
First remove the action from form
if your server side code is in the same page. And Use the empty()
or isset()
functions for checking the value.
如果服务器端代码在同一页面中,请首先从表单中删除操作。并使用empty()或isset()函数来检查值。
For Example:
if(!empty(trim($_POST['userName']))){
echo $_POST['userName'];
}
if(isset(trim($_POST['userName']))){
echo $_POST['userName'];
}
#2
2
if("" == trim($_POST['userName'])){
echo $_POST['userName'];
}
This is actually checking if the value is empty and echoes it if it is.
这实际上是检查值是否为空并且如果是,则回显它。
You probably want to use !empty($_POST['userName'])
to check if it's not empty and then echo it if it is not.
您可能想要使用!empty($ _ POST ['userName'])来检查它是否为空,然后如果不是则回显它。
#3
2
try this:
HTML code
<form action="thispage.php" method="post">
Name: <input type="text" name="userName" id="userName" />
<input type="submit" name="submit" />
</form>
PHP code on the same page:
同一页面上的PHP代码:
if(isset($_POST['submit']))
{
if(isset(trim($_POST['userName']))){
echo $_POST['userName'];
}
}
#4
1
Try this...
if(trim($_POST['userName']) != ' '){
echo $_POST['userName'];
}
#1
4
First remove the action from form
if your server side code is in the same page. And Use the empty()
or isset()
functions for checking the value.
如果服务器端代码在同一页面中,请首先从表单中删除操作。并使用empty()或isset()函数来检查值。
For Example:
if(!empty(trim($_POST['userName']))){
echo $_POST['userName'];
}
if(isset(trim($_POST['userName']))){
echo $_POST['userName'];
}
#2
2
if("" == trim($_POST['userName'])){
echo $_POST['userName'];
}
This is actually checking if the value is empty and echoes it if it is.
这实际上是检查值是否为空并且如果是,则回显它。
You probably want to use !empty($_POST['userName'])
to check if it's not empty and then echo it if it is not.
您可能想要使用!empty($ _ POST ['userName'])来检查它是否为空,然后如果不是则回显它。
#3
2
try this:
HTML code
<form action="thispage.php" method="post">
Name: <input type="text" name="userName" id="userName" />
<input type="submit" name="submit" />
</form>
PHP code on the same page:
同一页面上的PHP代码:
if(isset($_POST['submit']))
{
if(isset(trim($_POST['userName']))){
echo $_POST['userName'];
}
}
#4
1
Try this...
if(trim($_POST['userName']) != ' '){
echo $_POST['userName'];
}