1

I am having trouble with the changing background color of input fields. If it is empty or already in use or the some another rule that I defined, the background color should change. Juts I need an example on simple form like:

<form name="login" action="" method="post">
Username: <input type="text" name="username"><br />
<input type="submit" name="submit" value="Submit">
</form>

I want to use PHP and CSS codes for to do that. Feel free to use div classes and other stuff.

Thanks

1
  • 3
    I would recommend to use jquery to do this, php is not so dynamic as jquery, try with focus, blur, onchange, onkeyup, etc. Commented Aug 23, 2014 at 21:22

2 Answers 2

1

You can do this with CSS3 attribute selectors. For example, if you wanted the background to be red if the input is empty or green otherwise.

input { background: green; }
input[value=""] { background: red; }

Note that CSS3 attribute selectors are not fully compatible with IE8 and lower. All other modern browsers will support them.

Also if you want it to update dynamically (ie. change from red to green when the user enters something into the input box) you would have to use Javascript.

Sign up to request clarification or add additional context in comments.

2 Comments

What if the user name is already registered ? Field is not empty but the username is already in use.
There are numerous options to be do that. The general flow would be submit your form, check the database for duplicates, if there are duplicates then output some error messages and the form again with css classes on the input fields. You should try it first, then post your code so people can give you more specific advice.
0

i wrote you a litte example

css

.placeholder {color:#aaa;}
input[type=text],
input[type=password] {
    background:#eee;
}
input[type=text]:focus,
input[type=password]:focus {
    background:#fff;
}

js

$("#username").focus(function(){
    if($(this).val() == "Benutzername")
    {
        $(this).val("");
        $(this).removeClass('placeholder');
    }
});
$("#username").blur(function(){
    if($(this).val() == "")
    {
        $(this).val("Benutzername");
        $(this).addClass('placeholder');
    }
});

$("#password").focus(function(){
    if($(this).val() == "Passwort")
    {
        $(this).val("");
        $(this).prop("type", "password");
        $(this).removeClass('placeholder');
    }
});
$("#password").blur(function(){
    if($(this).val() == "")
    {
        $(this).val("Passwort");
        $(this).prop("type", "text");
        $(this).addClass('placeholder');
    }
});

html

<input id="username" type="text" class="placeholder" value="Benutzername" /><br />
<input id="password" type="text" class="placeholder" value="Passwort" />

Check it out in JSFiddle http://jsfiddle.net/hk8k4vew/

2 Comments

Its java script. I want only PHP and CSS as I wrote the question.
then you have to add classes to the inputs with PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.