Forum Moderators: coopster

Message Too Old, No Replies

is there a way of doing this?

         

al1911

5:47 am on Jan 24, 2005 (gmt 0)

10+ Year Member



i am passing a form to a php page
i want to display different content depending on the result of the form
the forum is called 'purpose', so...

$purpose = $_POST['purpose'][
if ( $purpose == "hello" ) { echo "Hello!"; }

but when the site first loads, the page is loaded - without being accessed from any form. how do i tell it to do something if no form result exists? in other words, can i go...

if ( $puspose == nothing ) ...

of is there a better way?
any help appreciated

ara818

7:29 am on Jan 24, 2005 (gmt 0)

10+ Year Member



sure, do this:

if(is_null($_POST['purpose'])) {
blah blah blah
}

thirdlight

10:44 am on Jan 24, 2005 (gmt 0)

10+ Year Member



I like :

if (!isset($_POST['purpose']) {
blah
}

which means you can have:
a: 'purpose' left deliberately empty
& b: the form not yet submitted

as 2 separate outcomes.

coopster

12:51 pm on Jan 24, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, thirdlight.

An often used approach is to test for the variable's existence and setting it to a default if it is not set.

$purpose = (isset($_POST['purpose'])) ? $_POST['purpose'] : '';
See the Ternary Operator [php.net] for an explanantion of how this works.

al1911

2:49 pm on Jan 24, 2005 (gmt 0)

10+ Year Member



thanks guys,
i should have said what i'm doing is... i only want to echo some text if the $_POST doesn't exist because it hasn't been passed
the isset thing is exactly what i was after
it all works now
thanks again :)

al1911

2:51 pm on Jan 24, 2005 (gmt 0)

10+ Year Member



BTW, thirdlight, you left of a closing bracket on the isset
caught it though

ara818

8:06 am on Jan 25, 2005 (gmt 0)

10+ Year Member



Just for completeness,
I believe is_null is the exact inverse of isset, as shown in [us3.php.net ]

So you can use either effectively with AFAIK no functional difference. Or am I missing something?

thirdlight

12:21 pm on Jan 25, 2005 (gmt 0)

10+ Year Member



Can't argue with that...

And thanks, al1911, for the ) - wasn't meant to be a test!