Hi guys, I'm looking for comments and suggestions regarding the use and place of jquery/ajax in site development.
I know jquery allows a lot of cool stuff and so does ajax, but when a site becomes heavily based on these tools, the use of PHP dwindles. I am used to PHP being the engine of a website but in order to use Ajax, a lot of site issues are handled in javascript instead.
I can inject php variables into javascript code because PHP is interpreted first, but at the same time, any new dynamic content that comes from javascript, PHP no longer controls or can handle, at least not without a page refresh.
When I use ajax to get dynamic content, I can't create URLs to return to these screens without a lot of finesse. When I use PHP for "dynamic" content, I can't really use javascript, because jscript doesn't do it's thing until after PHP is interpreted.
The problem is I'm starting to get frustrated trying to use the "coolness" of javascript but maintain a solid PHP core. In PHP where I would normally include another file to run as-is, to use ajax I have to include files that output html or json data to parse. So I find myself having some files here for PHP-only processing and some files there for PHP/JSON/AJAX processing. I have elements on the page that are setup with PHP code and then other elements that are managed by jquery dynamics. To me it seems to be getting messy. Every time I want to do something with javascript it seems like I have to add yet another library to include. Oh you want to change URLs without refreshing? Library. Oh you changed the contents of a table and now the css styles aren't applied to the new content? Library. Oh you want to make sure these javascript features are enabled first, how about adding modernizr? Do I really need 6000 lines of javascript code added to my site just to have a few dynamic tables or proper URLs? It's annoying. And what about graceful degredation? If javascript isn't available I can check for that in strait HTML and display something, this is after PHP. So now I've got to check for features in PHP, HTML, and Javascript and try to degrade out of all of them.
If I use PHP error handling, it is "above" javascript so I can't throw PHP exceptions with jscript which means I need multiple layers of error handling where PHP errors branch this way and jscript errors branch that way.
If I have something like a dropdown box for a selection and I use jquery/ajax to perform functions based on the selection, I can't save that page so it's bookmarkable in the sense that a user can go back to the page with the selection already there, not without a whole bunch of jscript wackiness.
If I DO put something in the URL then now I have to duplicate code, one set of code for PHP to handle the GET urls and display something like a table, and then another set of jquery functions to handle the same table if an on-page element is used instead. For example there is a page where a user can populate a table based on either a drop-down(parsed by jquery/ajax) or by typing a search term and submitting (handled by PHP). But when they use the drop-down box, the URL doesn't change, if they search, the URL has the search term included. So say they search for something, but then use the drop-down to change it, if they refresh the page it goes back to the search term and not the drop-down!
I've found a way using HTML5 and history (yeah another dependancy!) but it's limited support and anyway it forces javascript to now be my content manager. In other words, jscript has to be the new content manager instead of PHP, or I have to bounce back and forth between which one is loading up content for which particular page or even element.
Honestly I'm not finding that building a site with PHP+jquery/ajax to be very friendly at all! And I don't want the site completely broken without javascript, so it has to fall back, but it can't if jscript starts managing URLs and history and content loading, that is PHP's job! But if PHP does it, the site is actually LESS functional when I enable cool dyanmic jquery features. ARG!
Not to mention how jscript and PHP don't share variables, I would use PHP for my session, but no javascript, perhaps cookies, I dunno. Javascript doesn't access my PHP configuration arrays. Nor can javascript call my PHP functions. Instead, I can only use ajax, which means I need all my PHP classes and functions to either output directly such as to an object or array, OR output html or ajax depending on who is calling for what data for what reason. So I'm having to reengineer all my PHP to do duplicate things, one for PHP, one for jscript.
It really can't be this difficult can it? Websites used to be some markup+content with styles. Now they are turning into these conflated multi-language programs with a crap load of dependancies and backwards compatability testing and graceful degredation and feature-checking forked functionality branches and "markup" is now hidden inside ajax calls, loops and echos an .append() and content is now split in various kinds of static files, templates, database calls and array-parsing loops.....I'm getting tired of this! Building a website is not so much about "proper markup" and elegant styles, it's now about programming, error handling, callbacks, code stacks, data logic and program flow etc...
Can I really get PHP and Javascript to play nice or do I need to learn some new languages?
Thanks for listening to my rant, now back to programming...