Checking if form has been submitted - PHP

PhpFormsValidation

Php Problem Overview


What is the best way of checking whether or not a form has been submitted to determine whether I should pass the form's variables to my validation class?

First I thought maybe:

isset($_POST)

But that will always return true as a superglobal is defined everywhere. I don't want to have to iterate through each element of my form with:

if(isset($_POST['element1']) || isset($_POST['element2']) || isset(...etc

Whilst writing this question I thought of a much more basic solution, add a hidden field to act as a flag that I can check.

Is there a 'cleaner' way to do it than adding my own flag?

Php Solutions


Solution 1 - Php

For general check if there was a POST action use:

if (!empty($_POST))

EDIT: As stated in the comments, this method won't work for in some cases (e.g. with check boxes and button without a name). You really should use:

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

Solution 2 - Php

How about

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

Solution 3 - Php

Actually, the submit button already performs this function.

Try in the FORM:

<form method="post">
<input type="submit" name="treasure" value="go!">
</form>

Then in the PHP handler:

if (isset($_POST['treasure'])){
echo "treasure will be set if the form has been submitted (to TRUE, I believe)";
}

Solution 4 - Php

Use

if(isset($_POST['submit'])) // name of your submit button

Solution 5 - Php

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

Solution 6 - Php

Try this

 <form action="" method="POST" id="formaddtask">
      Add Task: <input type="text"name="newtaskname" />
      <input type="submit" value="Submit"/>
 </form>
    
    //Check if the form is submitted
    if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['newtaskname'])){
        
    }

Solution 7 - Php

On a different note, it is also always a good practice to add a token to your form and verify it to check if the data was not sent from outside. Here are the steps:

  1. Generate a unique token (you can use hash) Ex:

    $token = hash (string $algo , string $data [, bool $raw_output = FALSE ] );
    
  2. Assign this token to a session variable. Ex:

    $_SESSION['form_token'] = $token;
    
  3. Add a hidden input to submit the token. Ex:

    input type="hidden" name="token" value="{$token}"
    
  4. then as part of your validation, check if the submitted token matches the session var.

    Ex: if ( $_POST['token'] === $_SESSION['form_token'] ) ....
    

Solution 8 - Php

I had the same problem - also make sure you add name="" in the input button. Well, that fix worked for me.

if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['add'])){
    echo "stuff is happening now";
}

<input type="submit" name="add" value="Submit">

Solution 9 - Php

You could also use:

is_array($_POST)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnonymousView Question on Stackoverflow
Solution 1 - PhpmatinoView Answer on Stackoverflow
Solution 2 - PhpOlafView Answer on Stackoverflow
Solution 3 - PhpTzshandView Answer on Stackoverflow
Solution 4 - PhpRikeshView Answer on Stackoverflow
Solution 5 - PhpCodeCasterView Answer on Stackoverflow
Solution 6 - PhprizonView Answer on Stackoverflow
Solution 7 - PhpProfessorView Answer on Stackoverflow
Solution 8 - PhpJenView Answer on Stackoverflow
Solution 9 - PhpSW4View Answer on Stackoverflow