0

I am (mostly) a front-end developer working on a prototype with a backend guy on a site. The basics of it would require a user login area, as well as a search form that would search and return results from a database table.

He is doing the backend logic with Java and PostgreSQL. He proposes to return a JSON format to me upon a query. This means I will have to take the data from the JSON string and populate/create the HTML markup. I can do this with either Javascript, or PHP. It seems to be that PHP would be a no brainer as I don't need to create HTML markup with Javascript/jQuery and also all the data populated by the server already, reducing the load on the client side, but this means as a "front-end" person I am also writing PHP.

And regarding loading all server data onto a page with Javascript, is this standard practice? Or should it only be used on AJAX?

Should the backend guy be generating the markup as well? What's the best way of separating this frontend and backend work? THanks!

3 Answers 3

1

If you are using PHP, you are a backend-guy, too.

If the markup is generated by the server, than you would usually not write an AJAX-application, because the markup is generated by the server.

In fact if you want to write a ajax-application, you have to manipulte the DOM with Javascript. Use jQuery or something like that to do this.

Seperating frontend and backend is done by creating an Interface, a contract which will separate the UI from the Backend-Logic. In your case the contract is the format of your JSON Data.

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

6 Comments

I am reluctant to be writing PHP, because that would mean I would be writing the backend presentation logic too correct? Also, "If the markup is generated by the server, than you would usually not write an AJAX-application, because the markup is generated by the server." Can't the markup be generated with PHP, and the Javscript in the AJAX just manipulate the DOM? In my mind I was hoping that he would write some sort of templating engine and deal with the presentation logic with maybe php. I will strictly be writing HTML/CSS and JS for any form of user interaction.
Yes, but if you want to change the UI, you can send a call to the server, which will generate the markup (Non-AJAX-Way) or make an asynchronious call to the server to call for the JSON Data od than changing the DOM with JavaScript. I would use the second approach.
Sorry by second approach do you mean JS or php?
No, you don't create markup with Javascript like document.write('<div>text</div>') you create the HTML indirectly by calling something like document.createChild('div').setText('text'). how to actually do this is another question. The answer depends on the JavaScript-Framework. (Maybe I misunderstood you)
yes I would be using jQuery and no I won't be using document.write but jQUery's DOM methods. This page on Stackoverflow /questions/2307535/creating-html-php-server-side-vs-jquery-client-side shows that the markup in JS is much more lengthy compared to php. I guess going back, the fundamental question is, should we be using JSON to pass data for ALL dynamic content? Because this is what he proposes. In my mind JSON is only used in AJAX calls.
|
0

A good compromise would be to do either:

Option 1

A small PHP script server-side which formats the restults into a table with appropriate Ids/similar to allow javascript to add classes for styling. This entire table could be returned via an AJAX call and placed within a placeholder div on the page.

Option 2

The server returns simple JSON to the front-end, the front-end uses whatever mechanism it sees fit to build the appropriate HTML

The first one is a little cleaner to code - The generation of the HTML is seperated from the styling, but it's an extra hop (the PHP) and is slightly inflexible - the JS can modify the table as appropriate but it's limited by the html PHP sends.

The second is slightly more verbose to code but completely flexible.

3 Comments

I guess my another question is, is it common to have all server data returned using JSON?
The honest answer is that it varies. I personally prefer to generate the html server-side in most cases as it results in cleaner code - I just need to make sure I follow conventions. I still apply all the styling client-side. For small pieces of data (checking username isn't already in use, etc... I'd use JSON). I suppose it really comes down to: Does your javascript need to understand/manipulate your results or just dump then on a page?
I think just dump them onto a page. The reason the backend guy proposed sending JSON was to separate the logic from the presentation code. I was just wondering if there would be better ways of such separation than using JSON to pass ALL data from the server. I have always thought that JS was meant to modify UI behavior and not actually generate HTML content (even though it can), but it seems now I have to create a motherload of HTML with JS/jQuery.
0

I send the data from the server to the browser in JSON all the time, format on the browser with templates of one form or another. I would rather work with arrays in Javascript as the array methods like map and filter make it much easier.

1 Comment

what are these templates you speak of? are these are just templates you build on your own?

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.