1

I have a php file, in it the code is bellow:

<?php

if (isset($_GET['action']) && $_GET['action'] == 'view') {

    echo "get";

    print_r($_GET);

} else {

}
?>

<html>

    <form method="get" action="<?php echo ($_SERVER['PHP_SELF'] . '?action=view&id=4') ; ?>">
        <input type="text"  />
        <input type="submit" />
    </form>

</html>

If I click the submit, why the echo "post"; and print_r($_POST); do not output to the screen?


EDIT01

I changed my code to bellow:

<?php

if (isset($_GET['action']) && $_GET['action'] == 'view') {

    echo "if ";
    var_dump($_GET);

} else {
    echo "else ";
    var_dump($_GET) ;
}

?>

<html>

    <form method="get" action="?action=view&id=4">
        <input type="text" name="username"  />
        <input type="submit" />
    </form>

</html>

however, it do not go through the if, it go else.

the screen output is like this:

enter image description here

sof-03
  • 2,255
  • 4
  • 15
  • 33
  • 1
    Because action is a GET var. – Lawrence Cherone Jul 25 '18 at 03:37
  • Use hidden input fields if you want it to process as a `POST`. Also escape the `$_SERVER['PHP_SELF']`. See https://stackoverflow.com/questions/6080022/php-self-and-xss for more info – user3783243 Jul 25 '18 at 03:40
  • @LawrenceCherone Sorry about that, i have changed to the GET method. program will not execute the if code, it execute the else. – sof-03 Jul 25 '18 at 03:55
  • @LawrenceCherone See my EDIT-01, bro, why it do not go through the `if` block? – sof-03 Jul 25 '18 at 04:08
  • Its related to: https://stackoverflow.com/questions/1116019/submitting-a-get-form-with-query-string-params-and-hidden-params-disappear as @user3783243 has already said use hidden params, or use POST for the form: https://3v4l.org/16sCo – Lawrence Cherone Jul 25 '18 at 04:29
  • @LawrenceCherone Ah, I see now. – user3783243 Jul 25 '18 at 04:35
  • That's a good dup. Didn't know HTML5 changed the functionality of the form with GETs https://stackoverflow.com/a/9882750/3783243. – user3783243 Jul 25 '18 at 04:58

1 Answers1

-1

Take a look at this code $_GET['action']. This code will seek the tag with name=action. Since there is no tag with name=action, your code will enter else instead of if.

To solve this, fix your input submit tag into this.

<input type="submit" name="action" />
david
  • 3,225
  • 9
  • 30
  • 43