Use PHP to hide an <input> element

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
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Use PHP to hide an <input> element

Post by Pavilion »

Hello...

I just want to hide an input element using php. After the php script runs, it returns information to the user in an echo. I don't want the user to use an input element until they've confirmed the returned echo information.

So... how do I return some css commands to hide this element along with the echoed information?

Thanks Much:

Pavilion
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Use PHP to hide an <input> element

Post by Celauran »

Could be as simple as having the input's class as a variable. Define a class with display: none and another with display: block (or inline or whatever) and set the variable based on the criteria of your choosing.
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Use PHP to hide an <input> element

Post by Pavilion »

Hello Celauran:
Could be as simple as having the input's class as a variable. Define a class with display: none and another with display: block (or inline or whatever) and set the variable based on the criteria of your choosing.
In principal I understand what you are suggesting. But... defining the variable using html, css, and php is a new concept. Do you know where there might be a tutorial, or example, regarding this strategy?

Thanks again - Pavilion
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Use PHP to hide an <input> element

Post by Celauran »

You define your two CSS classes as normal.

Code: Select all

.hide
{
    display: none;
}
.show
{
    display: block;
}
In your PHP code, you just need to determine which applies.

Code: Select all

$class = (some condition) ? 'show' : 'hide';
<input type="text" class="<?php echo $class; ?>" etc />
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Use PHP to hide an <input> element

Post by Pavilion »

Celauran wrote:You define your two CSS classes as normal.

Code: Select all

.hide
{
    display: none;
}
.show
{
    display: block;
}
In your PHP code, you just need to determine which applies.

Code: Select all

$class = (some condition) ? 'show' : 'hide';
<input type="text" class="<?php echo $class; ?>" etc />
Celauran... THANK YOU!!!!!

I'd found information on using variables with css yesterday, but everything I tried didn't work. Your little demo went a long way towards clarifying things. I'll give it a run tonight, and see if I can make it work in the application.

Pavilion
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Use PHP to hide an <input> element

Post by califdon »

Do understand this: there are no "if"s in HTML or CSS because they are not logical processing languages, they are just markup languages. PHP is a logical processing language and it is the only thing you can use (other than Javascript, in the browser) to do conditional logic. PHP's function is to determine, before sending anything to the browser, exactly WHAT will be sent to the browser. Think of it that way and you should be able to answer such questions.
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Use PHP to hide an <input> element

Post by Pavilion »

califdon wrote:Do understand this: there are no "if"s in HTML or CSS because they are not logical processing languages, they are just markup languages. PHP is a logical processing language and it is the only thing you can use (other than Javascript, in the browser) to do conditional logic. PHP's function is to determine, before sending anything to the browser, exactly WHAT will be sent to the browser. Think of it that way and you should be able to answer such questions.
Hello Califdon -

Yes - your observation - is something I have noticed. It's percisely why I've felt so much frustration in getting one single input element to "hide" when returning data from php. But.. Celauran's suggestion should take care of the trick. I'm going to try working with it right now.

Pavilion
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Use PHP to hide an <input> element

Post by Pavilion »

Hello Celauran:

Your solution is fantastic. Such a small thing, can make such a big difference.

It's best to show users only what they need to complete a task, and hiding that one input element until a user confirmed data returned by php makes a big difference.

Thanks again:

Pavilion
Post Reply