Reputation: 5147
I am using PHP/jQuery/MySQL to build a small hotel website for my friend. Like a typical CRUD app it will have forms and reports. I have a PHP framework in place to render forms and save them using Ajax. Now I need to update the elements on webpage when a data item is saved. As this is a small project I would have gone with simple approach of sending HTML in Ajax response and embedding the response in webpage. But I decided to use JSON so that I can extend the framework to other projects. As of now following is my design idea:
{
'status':'', /*status like success/fail */
'message':'', /*a status message or sting will be more helpful in case of failure */
'data':'', /*arbitrary data*/
'datatype':'' /*is it JSON or HTML or javascript( or some thing else?)*/
}
Based on response jQuery will update/manage webpage elements. Am I missing some thing here or am I totally wrong in this design.
Upvotes: 1
Views: 96
Reputation: 53
I would not have status in there and would instead look at using the HTTP status codes to report back status. In PHP you can use header()
to pass back a 400 if you get and issue. If you then pass back 200 if everything is OK.
Then in jQuery you can do:
$.getJSON( 'http://someUrl/action.php' ).success( function() {
alert( 'success' );
} ).error( function() {
alert( 'fail' );
} );
For you JSON structure I would then keep everything else you have.
{
'message': '', /*a status message or sting will be more helpful in case of failure */
'datatype': '', /*is it JSON or HTML or javascript( or some thing else?)*/
'data': {} /*arbitrary data*/
}
I hope that helps.
Upvotes: 2