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...

diafol commented: I get you +14

Recommended Answers

All 5 Replies

Member Avatar for LastMitch

Where is the balance between PHP and Jquery/ajax for site dev?

I prefer to used PHP/MYSQLi on the websites having Jquery/Ajax is not bad. It really depends what you are trying to design.

The company I work for is developing a new application that primarly works on a jquery/ajax front end with php doing the communcation back and forth from the database. So far we have had no problems.

Despite what you may have heard, jquery current verrsion are quite solid. I've had few problems interms of stablility. Truth be told if you plan on running a mass traffic application you want more of logic being handled by jquery. This means most of the work is being done in the client side, there fore leaving less stress on your servers. Now with a small application im sure this wouldnt very much matter.

But given the choice,Id take the cool effects and less stress of my sever. Over code consistency.

I wont argue at all the more ajax calls the slower your pages load. However if you plan on doing these same actions just with php id say you rather having that stress on the client not risking the overall speed of our servers.

But hey its going to be up to you.

If i had to complain about one thing with jquery, is certian add ons lack uniform codeing practices . Case and point jqGrid with some options being case sensative and others not.

Given me quite a few headaches :-P

Also keep in mind jquery can do something that php cant ;)

Member Avatar for diafol

I recognize the feeling. As a one-man hobbyist, I tend to write a lot of spaghetti as front-end ideas jump to mind as I'm coding back-end - regardless of a pretty good planning routine. I have little discipline, so I start to mess with jQuery, and oh, then of course the html comes in for a slamming with some class hooks, which inevitably makes we want to splash out on a new css file.

Websites used to be some markup+content with styles.

Some still are, but they aren't particularly engaging anymore. I think users have moved on. Of course some sites you actually want them to be like that, but sites wanting to maintain a presence need a degree of silent dynamism and interactivity.

I remember when javascript died a death and it was all back-end stuff with a million css workarounds for different IE versions and horrible flash movies. Great for PHP devs! But, js is back and with a vengeance - jQuery and others have brought interactivity back to the masses.

Is the web becoming far more technical to the average webdev/designer? IMO yes, but that doesn't mean it's a bad thing. We will always have workarounds/shivs etc for compatibility issues, but look at what we're able to do now as opposed to 10 years ago. RWD - this would have been deemed impossible 5 years ago. However, as you state, there will be a blurring around responsibility and delegation. Do we use CSS for transitions or jQuery? That date you wanted displaying (unix timestamp data from db), shall we use jQuery or PHP to convert it?

IMO, I'd use PHP for everything possible, but that's not a correct answer. I think you use the best tool for the job. With your own projects, you're in control, so you make the decisions. Your sites will be as complicated or as simple as you decide they need to be. I like having to learn new stuff. I wouldn't get far without loads of resources, but the ability to write markup and code in a number of languages is extremely satisfying.

Can I really get PHP and Javascript to play nice or do I need to learn some new languages?

The only time I mix them is when I want to pass json data from a DB, so it's no big deal. I don't really count ajax.

Tried Java?

There is a clear barrier, don’t server content ONLY with JS. That doesn’t mean actually that you can’t change the content of the page , but if you do be sure that you serve the same content in a page (without JS).

The second barrier I always have in mind isn’t as clean as the first one. Don’t annoy users with AJAX, jQuery and JS. There is a fragile balance for an app to be interactive and useful from being just annoying. To be honest I can’t understand this balance when I am developing, but for my good luck I have a demand department that performs beta tests and we always disagree in this one. Some times after long time if I see an application of mine as a user and the balance isn’t right I understand it…

One of the biggest problems is the fact that we can't count on javascript. In other words, I could potentially do everything in jquery with ajax and use php on the backend to serve up data only, but then all a person has to do is turn off jscript and the site falls apart or at worst opens security holes. So wasting time building everything with jscript just to have to have a backup "old school" way with php doing all the same stuff, seems like building two complete sites mingled together in a mesh of ugly.
Yes it's nice to have content pop up without a page refresh, but then you can't bookmark to this page, and if jscript is off nothing works without a backup route.
And even that is messy because you can test for lack of jscript in html </noscript> type thing but that's client-side so now I need client side to test jscript then send back to server and make php take over and bla bla. Three Stooges running a website it would seem.

Here is a case-in-point. I have a page for adding some data to a database, it uses a standard <form> and submits to a php handler, the php returns them to the referring page, whatever that might be. The problem is, the location or part of the database where the data is uploaded is actually set by jquery. For example they pick from a dropdown box for a value, jquery loads data in a second dropdown, pick that list next, then the form will submit data based on those selections. The problem is when the data is submitted, I can't send them back to the page WITH those two selections already selected. So in other words, if I wanted to be able to rapidly submit to the same two selections, I have to keep selecting from the dropdown boxes rather than starting from the same selection again.
You can imaging the first dropdown could be like selecting books|dvds|magazines|etc. Pick one then the second drop down is populated with action|adventure|suspense|etc. Once they've picked two selections, the form submits data to that part of the DB. But after submission, I can't send them back to the dvds->action page. Not without having a dual system of jquery+php url management. Jquery is cool non-refresh, but if I also add to URL like "?type=dvd&cat=action", this would be php, and would need secondary parsing from the jquery one.
I love the idea of DRY code, and can't stand it when I have to repeat myself in code, with multiple languages, to do the same thing. Or even worse, having a jquery loop over here to build a table, but repeating the same logic in PHP somewhere else to build the table in case jscript is off. Uuuuuugly!

I guess I'm going to have to re-engineer the site so that PHP is only for URL routing and backend data assembly. If so, I have to rely on javascript for front end, which means a simple browse setting or an out of date version could break the whole site. What other option is there? No more fancy jscript I suppose?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.