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" />

Jiew Meng
  • 74,635
  • 166
  • 442
  • 756
  • Do you want to check if there is `post` data present, or if the `submit` data is present? – Ben Nov 22 '10 at 03:39
  • Checking for a post or post back? A post back is a particular type of post request. – Lèse majesté Nov 22 '10 at 04: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 '10 at 04: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. – Craig May 09 '14 at 17:23

5 Answers5

26

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'])
David Murdoch
  • 82,194
  • 38
  • 141
  • 186
BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
  • 1
    You should definitely use `===` instead of `==`. Explanation, why, may be found in comments to [this answer](http://stackoverflow.com/a/1372163/1469208) and in many, many other resources, as these are fundamentals of PHP programming. – trejder Feb 24 '14 at 09: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.

Community
  • 1
  • 1
Steven
  • 17,136
  • 12
  • 61
  • 113
  • You should definitely use `===` instead of `==`. Explanation, why, may be found in comments to [this answer](http://stackoverflow.com/a/1372163/1469208) and in many, many other resources, as these are fundamentals of PHP programming. – trejder Feb 24 '14 at 09: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

dejjub-AIS
  • 1,369
  • 1
  • 20
  • 49
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.

Valentin Flachsel
  • 10,595
  • 10
  • 39
  • 64
0

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

Inigoesdr
  • 1,439
  • 11
  • 8