16

How can I check if the request is a post back in PHP, is the below ok?

if (isset($_POST["submit"]))

where submit is the name of the <input type="submit" />

4
  • Do you want to check if there is post data present, or if the submit data is present?
    – Ben
    Nov 22, 2010 at 3:39
  • Checking for a post or post back? A post back is a particular type of post request. Nov 22, 2010 at 4:06
  • @Steve, @Lèse majesté, basically, I just want to see if a form is post back so I process the post back
    – Jiew Meng
    Nov 22, 2010 at 4:48
  • I feel the moderators got a little gung-ho about marking this as a duplicate. This answer is more useful than the alleged "original" in terms of showing someone how to actually and simply detect a POSTBACK to the same page using PHP, which is what the OP's question seems to be asking for. May 9, 2014 at 17:23

5 Answers 5

27

That will work if you know and expect such a submit button on the same page.

If you don't immediately know anything about the request variables, another way is to check the request method:

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

As pointed out in the comments, to specifically check for a postback and not just any POST request, you need to ensure that the referrer is the same page as the processing page. Something like this:

if (basename($_SERVER['HTTP_REFERER']) == $_SERVER['SCRIPT_NAME'])
1
  • 1
    You should definitely use === instead of ==. Explanation, why, may be found in comments to this answer and in many, many other resources, as these are fundamentals of PHP programming.
    – trejder
    Feb 24, 2014 at 9:41
3

You want $_SERVER['REQUEST_METHOD'] == 'POST'.

Yours is a very similar although less general question than this one.

This is probably a better approach than actually checking a post variable. For one, you don't know whether that variable will be sent along. I have the hunch that some browsers just won't send the key at all if no value is specified. Also, I'd worry that some flavors of PHP might not define $_POST if there are no POSTed values.

1
  • You should definitely use === instead of ==. Explanation, why, may be found in comments to this answer and in many, many other resources, as these are fundamentals of PHP programming.
    – trejder
    Feb 24, 2014 at 9:41
3

If you want to have a generic routine without dependency "method" (post/get) and any other names of the forum elements, then I recommend this

<?php
$isPostBack = false;

$referer = "";
$thisPage = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

if (isset($_SERVER['HTTP_REFERER'])){
    $referer = $_SERVER['HTTP_REFERER'];
}

if ($referer == $thisPage){
    $isPostBack = true;
} 
?>

now the if $isPostBack will be true if it is a postback, false if not.

I hope this helps

1

Yes, that should do it.

Careful when you're using image type submits, they won't send the name attribute in some browsers and you won't be able to detect the POST. Smashed my head against the desk a few times until I realized it myself.

The workaround for that is to add a hidden type input as well.

0

Yes. You could also use if(array_key_exists('submit', $_POST))

Not the answer you're looking for? Browse other questions tagged or ask your own question.