我想用PHP检查$_POST["pass"]是否设置了,如果没有,就做点什么,如果是,就做别的事情......但我无法让它工作,我确定我的逻辑是错误的。
我有一个看起来像这样的php代码...
if (!isset($_POST["pass"])) {
...some form with an input type text here...
if (...wrote the wrong thing in input type text...) {
echo "something is wrong....";
}
else {
$pass_var = "Pass";
$pass_var = $_POST["pass"];
}
}
else {
echo "This thing is working...";
}
如果我在输入类型的文本中输入正确的东西,我不想得到"这个东西正在工作",如果不是,我不想回声"有问题......"。
它几乎可以正常工作,除了如果我在我的表单中键入正确的东西,我永远不会得到"这个东西正在工作......"。
该页面什么都不做..
我敢肯定是
$pass_var = "Pass";
$pass_var = $_POST["pass"];
我做错了。
我知道我可以用另一种方式设置它来让它工作,但我有一个像这样设置的大型脚本,我真的希望它工作......
您在表单中针对未设置的 $_POST 进行测试(请参阅 !)。但是,您希望设置帖子!
if(isset($_POST["pass"]))
{
print_r($_POST); // basic debugging -> Test the post array
echo "The form was submitted";
// ...some form with an input type text here...
if(...wrote the wrong thing in input type text...)
{
echo "something is wrong with the input....";
}
else
{
// Valid user input, process form
echo "Valid input byy the user";
$pass_var = "Pass";
$pass_var = $_POST["pass"];
}
}
else
{
echo "The form was not submitted...";
}
你可以使用 php 的empty()
函数
if(!empty($_POST['pass'])){
// do something
}else{
// do something else
}
希望这对你有用.
确保你的 html 表单中有"method='POST'",否则 $_POST 在 php 中无法访问,逻辑有点糟糕,试试这个吗?
例如
if (!isset($_POST["pass"])) {
//no POST so echo form
echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'>
<input type='text' name='txtInput' />
<input type='submit' name='pass' />
</form>";
} elseif (isset($_POST["pass"])) {
//have POST check txtInput for "right thing"
if ($_POST["txtInput"] == "wrong thing") {
echo "something is wrong....";
} elseif ($_POST["txtInput"] == "right thing") {
//$pass_var = "Pass";
$pass_var = $_POST["pass"];
echo "This thing is working...";
}
}
好吧,if (!isset($_POST["pass"]))
的意思是如果没有设置$_POST["pass"]
,所以你可能想删除代表not的"!"。