-2

I have inherited a php site that is returning HTML pages. I always thought that the server returns data to the client and the client decides how to show the results. Even though this is working, is this not a very tight coupling between the server and the client?

I would have thought a much better way to handle this is for the client code, javascript or gwt or what have you to ask for the needed data and the server returning that data only such as JSON object or a similar thing.

Thoughts on this?

reza
  • 5,972
  • 15
  • 84
  • 126
  • This really isn't a thoughts/discussion site. Once you have established what language you're using and have some code, and are stuck with something specific, you can ask and post the problematic code. You need a forum if you want to discuss pros/cons of languages and what's best for you :) – James Sep 04 '13 at 16:12
  • HTML is a data format for describing the semantics and structure of text (along with relationships to other resources). It is up to the client to decide how to render it. CSS provides a way for an author to provide a suggested rendering. – Quentin Sep 04 '13 at 16:28
  • The web has a standard data format that browsers understand. Delivering a custom piece of software and then data in a less naturally expressive format is not better. – Quentin Sep 04 '13 at 16:53
  • I was not thinking custom format/protocol. I was thinking about JSON. – reza Sep 04 '13 at 17:04

2 Answers2

1

It sounds like you could benefit from making an AJAX call (via js) to a php page, then manipulating the data (JSON object, string of comma delimited data, raw HTML, etc.) returned on the client side.

Sorry for the bad previous example, this example is a more sophisticated, modern example of how an ajax call should be made.

Community
  • 1
  • 1
em_
  • 2,134
  • 2
  • 24
  • 39
  • Decent? It suffers from SQL injection, isn't progressively enhanced and uses 90's style markup like `onchange` attributes. No, that isn't decent. – Quentin Sep 04 '13 at 16:29
  • this was exactly my thought. php should return the data and the client deal with the rendering of the page. – reza Sep 04 '13 at 16:37
  • @Quentin I agree it wasn't the best, but I was more worried about getting the idea across than giving a good example. I have changed the link to a better example. – em_ Sep 04 '13 at 16:42
  • looking at the original example, if I am not mistaken the return is still an HTML page. Correct? – reza Sep 04 '13 at 16:59
  • The original example (w3schools reference) shows how an id of a user is passed to a php page, the php makes a query to the database asking for the information for the given user id. The php page then outputs a html table formatting the returned information. On the client side (assuming a successful call), all the data (in this case, raw text describing the html table with the user's information) that the php script returned (xmlhttp.responseText) will populate the element with id "txtHint". – em_ Sep 04 '13 at 17:09
  • sorry for being so dense, but in the above explanation, is the php script returning HTML forcing the client to display the data in a specific format? – reza Sep 04 '13 at 18:00
  • Kind of, the script is writing up a HTML TABLE element, this element describes a way of formatting. The client simply takes that table and puts it somewhere on the page. – em_ Sep 05 '13 at 12:11
1

It appears that it is not at all uncommon for php scripts to return HTML. It does create tight coupling with the client application. Returning JSON does create a more loosely coupling with the client.

reza
  • 5,972
  • 15
  • 84
  • 126