Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if $_POST is set?

Tags:

post

php

I want to know how to detect if $_POST is set or not.

Right now I detect it like this:

if(isset($_POST['value'])) 

But I'm not looking if value is set anymore. Basically, any POST will work.

if(isset($_POST)) 

I'm not sure how PHP handle this. Perhabs isset($_POST) is always returns true since it's a PHP global?

Basically, how can I do this?

like image 635
Lisa Miskovsky Avatar asked Mar 05 '13 09:03

Lisa Miskovsky


People also ask

How do I know if post data is set?

Check if $_POST Exists With isset() The isset() function is a PHP built-in function that can check if a variable is set, and not NULL. Also, it works on arrays and array-key values. PHP $_POST contains array-key values, so, isset() can work on it. To check if $_POST exists, pass it as a value to the isset() function.

Why $_ POST is empty?

In my case, when posting from HTTP to HTTPS, the $_POST comes empty. The problem was, that the form had an action like this //example.com When I fixed the URL to https://example.com , the problem disappeared. I think is something related on how the server is setup. I am having the same issues using GoDaddy as a host.

What is if ($_ POST in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.


2 Answers

Try with:

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

to check if your script was POSTed.

If additional data was passed, $_POST will not be empty, otherwise it will.

You can use empty method to check if it contains data.

if ( !empty($_POST) ) {} 
like image 125
hsz Avatar answered Sep 30 '22 03:09

hsz


$_POST is an array. You can check:

count($_POST) 

If it is greater than zero that means some values were posted.

like image 42
Salman A Avatar answered Sep 30 '22 02:09

Salman A