runeveryday
runeveryday

Reputation: 2799

How does this check whether the form was submitted?

HTML:

<form name="myform" action="process.php" method="POST">
    <input type="hidden" name="check_submit" value="1" />
    <!-- ........ -->
</form>

PHP:

if (array_key_exists('check_submit', $_POST)) {....}

Why can array_key_exists('check_submit', $_POST) check whether the form was submitted?

I've seen isset($_POST['...']) used before, but not this.

if i don't do this array_key_exists('check_submit', $_POST) decision., what may happen.

Upvotes: 0

Views: 768

Answers (4)

Bas Slagter
Bas Slagter

Reputation: 9929

Don't you mean something like:

 if(!empty($_POST) && isset($_POST['check_submit']) && $_POST['check_submit'] == '1'){
   // do something
 }

Upvotes: 0

gnur
gnur

Reputation: 4733

To check wether your page was called as a result of a form submit (via POST) you should use something like this:

if ($_SERVER['REQUEST_METHOD'] == 'POST')

You shouldn't use hidden form elements just to have a value to check wether a form was submitted. If you use the request method you catch it more cleanly and don't have any trouble if the form ids/names/values are altered.

Upvotes: 3

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

check_submit is a field in your form, so when you submit the form, that field is available in the POST data.

PHP places incoming POST-method form data into the $_POST superglobal array, and your code determines whether the check_submit field can be found in that array.

Indeed, it's quite similar to isset($_POST['check_submit']), in that it checks whether such an element exists in $_POST. It's just taking a slightly different approach.


If you did not submit the form, then of course there is no form data.

Upvotes: 1

DhruvPathak
DhruvPathak

Reputation: 43235

Do var_dump($_POST), you will see the $_POST associative array, which will have check_submit key, and value = 1

Upvotes: 0

Related Questions