Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if $_POST exists

Tags:

php

I'm trying to check whether a $_POST exists and if it does, print it inside another string, if not, don't print at all.

something like this:

$fromPerson = '+from%3A'.$_POST['fromPerson'];

function fromPerson() {
    if !($_POST['fromPerson']) {
        print ''
    } else {
        print $fromPerson
    };
}

$newString = fromPerson();

Any help would be great!

like image 876
eliwedel Avatar asked Aug 16 '10 20:08

eliwedel


People also ask

How do you check if there is a post in PHP?

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.

Which function is used to check if $_ POST variable is empty?

Use the empty function to check if a variable contents something: <?

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 isset ($_ POST in PHP?

The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.


3 Answers

if( isset($_POST['fromPerson']) )
{
     $fromPerson = '+from%3A'.$_POST['fromPerson'];
     echo $fromPerson;
}
like image 106
ehmad Avatar answered Oct 19 '22 15:10

ehmad


Simple. You've two choices:

1. Check if there's ANY post data at all

//Note: This resolves as true even if all $_POST values are empty strings
if (!empty($_POST))
{
    // handle post data
    $fromPerson = '+from%3A'.$_POST['fromPerson'];
    echo $fromPerson;
}

(OR)

2. Only check if a PARTICULAR Key is available in post data

if (isset($_POST['fromPerson']) )
{
    $fromPerson = '+from%3A'.$_POST['fromPerson'];
    echo $fromPerson;
}
like image 68
Dheeraj Bhaskar Avatar answered Oct 19 '22 16:10

Dheeraj Bhaskar


Surprised it has not been mentioned

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['fromPerson'])){
like image 35
John Magnolia Avatar answered Oct 19 '22 16:10

John Magnolia