PHP or JS?

Discussion in 'PHP' started by nickjason, Jul 26, 2009.

  1. #1
    Lets say I have this form field:

    HTML Code:

    <label for="title">Title</label><input type="text" name="title" id="title" value="" />

    Some common requirements I have for forms are filling fields with saved data from the request, and adding an error class to the label when an error has occured. Sometimes I might even want an error message next to the field.

    These would all take me seconds to do with jQuery. In order to degrade gracefully, however, it seems like this stuff should be done with PHP. That makes form code pretty ugly. I am not a fan of form generation. The code I posted above is easy enough to write, easy enough to read, and is easy for a designer to style.

    An idea I had was to have some sort of jQuery-ish selectors that could grab some html and modify it somehow (adding classes, adding attributes, changing attribute values, etc.) That would allow me to keep the simple markup, but would degrade gracefully in browsers with javascript disabled. Maybe that doesn't matter these days though? At what point should I stop trying to do everything with PHP, and rely on client side technology being available?
     
    nickjason, Jul 26, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I find it best to evaluate where one works best. PHP is obviously server sided, so you shouldn't attempt to use it after data had already been sent to the browser. And javascript is good for when the browser has already been loaded.

    However in some cases you can use both just fine, such as using Jquery's $.get() to call a PHP file and then insert that data into the html based on what the php script returns. As an example, the search bar on apple.com in the top corner, which automatically drops down search results with thumbnails, is a combination of Prototype+Scriptaculous (like Jquery), and PHP.

    Far as 'appearance', You really shouldn't need to modify any markups and such. CSS on most modern browsers is very capable. Javascript is best for events and such. But you can handle a good number of events such as hover and such with CSS even on browsers with JS disabled. The only thing you might have to drastically change the markup for is mobile browsers such as WAP enabled devices (the iphone/ipod can pretty much view anything safari on the desktop can, with the exception of flash), but by that point its best to just redirect them to the mobile version of the site.
     
    kblessinggr, Jul 26, 2009 IP
  3. nickjason

    nickjason Peon

    Messages:
    414
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thats true.
     
    nickjason, Jul 30, 2009 IP