Coder Perfect

Check for the existence of $_POST.

Problem

I’m attempting to see if a $_POST exists and, if so, print it within another string; else, don’t print anything.

something like this:

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

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

$newString = fromPerson();

Any assistance would be much appreciated!

Asked by eliwedel

Solution #1

if( isset($_POST['fromPerson']) )
{
     $fromPerson = '+from%3A'.$_POST['fromPerson'];
     echo $fromPerson;
}

Answered by ehmad

Solution #2

//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;
}
if (isset($_POST['fromPerson']) )
{
    $fromPerson = '+from%3A'.$_POST['fromPerson'];
    echo $fromPerson;
}

Answered by Dheeraj Bhaskar

Solution #3

Everyone says to use isset(), and it will most likely work for you.

However, it’s critical that you know the difference between the two.

$_POST[‘x’] = NULL, $_POST[‘x’] = “, $_POST[‘x’] = “, $_POST[‘x’] = “, $_POST[‘x’

In the first example, isset($ POST[‘x’]) returns false, while in the second, it returns true, despite the fact that both would yield a blank value if printed.

If your $_POST comes from a user-inputted field/form that is left blank, I BELIEVE (though I’m not 100% sure) that the value will be “” but NOT NULL.

Even if that assumption is erroneous (please correct me if I’m mistaken! ), The above information is still useful in the future.

Answered by Rafael

Solution #4

It’s surprising that it hasn’t been mentioned.

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['fromPerson'])){

Answered by John Magnolia

Solution #5

isset($_POST['fromPerson']) 

Answered by h3xStream

Post is based on https://stackoverflow.com/questions/3496971/check-if-post-exists