dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Can I use PHP to change the color of a text field when the user clicks it?

  1. #1
    Join Date
    Oct 2006
    Posts
    74

    Thumbs up Can I use PHP to change the color of a text field when the user clicks it?

    If you have a HTML form and have a text field that when the user clicks on this so that its the one which the user can input data in is there a way of PHP changing the color of the background of the text field?

    I would like to find a solution designed in PHP.

    If it is possible can someone please provide some example code and an explanation on why it works?

    Thanks!!

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Can I use PHP to change the color of a text field when the user clicks it?

    PHP is a server-side language. The onclick event is on the client-side. Using PHP would be a ridiculous implementation because it would require client-side scripting to communicate with the server-side PHP and then the server-side PHP would have to tell the client-side to change the color.

    All that to say...use the client-side!

    Code:
    <textarea ... onclick="this.style.background='#ffff00';"></textarea>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Nov 2009
    Posts
    31

    Re: Can I use PHP to change the color of a text field when the user clicks it?

    Quote Originally Posted by PeejAvery View Post
    PHP is a server-side language. The onclick event is on the client-side. Using PHP would be a ridiculous implementation because it would require client-side scripting to communicate with the server-side PHP and then the server-side PHP would have to tell the client-side to change the color.

    All that to say...use the client-side!

    Code:
    <textarea ... onclick="this.style.background='#ffff00';"></textarea>
    Actually you will get better speed out of change the class name and letting CSS change the style then the change the style direct (except in opera), but again a client side issue.

    http://www.quirksmode.org/dom/classchange.html

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Can I use PHP to change the color of a text field when the user clicks it?

    Quote Originally Posted by Roguebfl View Post
    Actually you will get better speed out of change the class name and letting CSS change the style then the change the style direct (except in opera), but again a client side issue.
    Yes and no. If you are changing multiple styles of the element, then yes. But, changing just the background, there's really no speed difference. In this case, if you're just changing the color, it would be more work and page load time to have two extra defined classes in the stylesheets.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Can I use PHP to change the color of a text field when the user clicks it?

    [ moved ]
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    Oct 2006
    Posts
    74

    Question Why does this CSS not work?

    I want to change the background color when the user clicks a input field and not all input fields on my form. I want to complete all of this using an external style sheet.

    I created some css but its not working! - any ideas on how to accomplish this using external style sheets?

    here is the css code :
    Code:
    .form
    {
    input:focus
    {
    background-color:#FF6666
    }
    }
    here is the html code:

    Code:
    <html>
    <link href="form.css" rel="stylesheet" type="text/css">
    <body>
    <table cols="2">
    <form name="form1">
    <tr>
    <td class=form><input name="txtName" size="30" class="form"></td>
    </tr>
    <tr>
    <td> <input name="txtSpace" size="30"> </td>
    </tr>
    </form>
    </table>
    </body>
    </html>
    Last edited by PeejAvery; December 28th, 2009 at 02:19 PM. Reason: Added code tags.

  7. #7
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Can I use PHP to change the color of a text field when the user clicks it?

    [ merged threads ]
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  8. #8
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Can I use PHP to change the color of a text field when the user clicks it?

    Your CSS tags are improperly nested.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Nov 2009
    Posts
    31

    Re: Can I use PHP to change the color of a text field when the user clicks it?

    Code:
    .form input:focus
    { background-color:#FF6666; }

  10. #10
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Can I use PHP to change the color of a text field when the user clicks it?

    And here goes a complete solution how to change attached css dynamically: http://www.alistapart.com/articles/alternate/
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that Iím 64-bit and my blanket is 32-bit and I couldnít cover myself with it, so Iíve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)