0

I want to use PHP to check if $_POST["pass"] is set, and do something if it's not, and do something else if it is.... But I can't get it working, I'm sure my logic is wrong.

I have a php code that looks something like this...

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...";
}

If I type the right thing in my input type text, I wan't to get to "This thing is working", and if not I wan't to echo "something is wrong....".

It works almost fine, except that if I type the right thing in my form, I never get to "This thing is working...".

The page just does nothing..

I'm sure it's the

$pass_var = "Pass";
$pass_var = $_POST["pass"];

that I'm doing wrong.

I know that I could set this up in another way to make it work, but I have a large script that is set up like this, and I really want it to work...

4

4 回答 4

2

您在表格中针对未设置的 $_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...";
  }
于 2013-10-16T07:13:02.060 回答
0

可以使用empty()php的功能

if(!empty($_POST['pass'])){
// do something
}else{
// do something else
}

希望这对你有用。

于 2013-10-16T07:34:22.643 回答
0

确保您的 html 表单中有“method='POST'”,否则在 php 中无法访问 $_POST,并且逻辑有点混乱,试试这个?

例如

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...";
   }
}
于 2013-10-16T08:19:10.507 回答
-2

好吧,if (!isset($_POST["pass"]))意思是如果$_POST["pass"]没有设置,所以你可能想删除'!' 代表不。

于 2013-10-16T07:08:33.400 回答