PHP elseif statement and dynamic fields.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rxroids
Forum Newbie
Posts: 5
Joined: Fri Apr 06, 2007 10:29 pm

PHP elseif statement and dynamic fields.

Post by rxroids »

I'm very new to PHP and so I've created a dynamic text field and when someone fills it out I want it to "echo" a response and what the person put in the field itself. But if the person left it blank I don't want it to "echo" anything. I've looked everywhere for an answer but I'm not even sure I'm using the right code or if I should even be using "elseif" statements.

Code: Select all

$editor = $_POST['editor'];
if( $editor = $_POST['editor'])
	{
	echo 'Ed. '.$_POST['editor'];
	}
else
	{
	echo '';
	}
Should I use another code?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP elseif statement and dynamic fields.

Post by Christopher »

Maybe:

Code: Select all

if ((isset($_POST['editor']) && ($_POST['editor'] != ''))
	{
	echo 'Ed. '.htmlentities($_POST['editor']);
	}
(#10850)
rxroids
Forum Newbie
Posts: 5
Joined: Fri Apr 06, 2007 10:29 pm

Thanx a lot

Post by rxroids »

A while ago I bought a PHP and MySQL web dev book and I used it to define your code. I ran it and it sent back an error so I debugged it and got the results I needed from the first part of your code, then I just plugged in my own thing. I never would have even thought of it if it weren't for that piece of code. Thanks a lot.

Code: Select all

if(isset($_POST['editor'])&&($_POST['editor']!= ''))
I'll be back soon...lol.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Nobody likes empty() for some reason

Code: Select all

if(!empty($_POST['editor']))
So much nicer to look at
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

aaronhall wrote:Nobody likes empty() for some reason

Code: Select all

if(!empty($_POST['editor']))
So much nicer to look at
I use empty() all the time.. It's much better than if (isset($) and $ != '') every single time
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yes it is. Empty actually checks isset() as well, I believe.

PS Arborints code had one too many opening parentheses in it. It wasn't bad code.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

nickvd wrote: I use empty() all the time.. It's much better than if (isset($) and $ != '') every single time
Except those situations when you actually do want to accept "0" string.
Post Reply