Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Say I have a model written in php whose properties are the columns of a table in a db and each instance of the model represents a row in the table. I also have a page ('view') where I display the contents of each column one row at a time, starting with the first row (id=1). Above this display are two buttons, one which when clicked displays the contents of the next row, and the other displays the contents of the previous row. I want the change of display to occur without reloading the page or going to a new page.

My question is simply would I need to use AJAX for this?

share|improve this question
2  
No, you will not need to make AJAX-style requests for this. Plain old javascript can manipulate the document and change the display just fine. –  GrandmasterB Jun 2 '13 at 5:12
    
How do I access the properties of the model that I need to display with javascript if the model is written in php? –  user89460 Jun 2 '13 at 5:17
    
Only thing I can think of is echoing all model instances and their properties with div tags whose id=$rowid, hiding them, and then doing something like: function(id){ getElementById('display').innerHTML = getElementById('rowid').innerHTML} but that seems overly clunky –  user89460 Jun 2 '13 at 5:26
    
If you want to access your php model each time a row is clicked by the user in a table in the browser, yes, you'd probably have to do AJAX style requests. You didnt say thats what you needed to do... only that you wanted to change the display, which does not require AJAX style requests. If the amount of data is modest, you can also just embed all the data into the page when it firsts loads and use javascript to alter the display from the data embedded into the page itself. –  GrandmasterB Jun 2 '13 at 5:43
    
Thank you; the amount of data is modest so I might do just that. –  user89460 Jun 2 '13 at 5:49
add comment

1 Answer

up vote -2 down vote accepted

I do pretty much the same things that you have mentioned in your question. What I follow @ work is:

  • We use jQuery as javascripting framework. it helps us seamlessly write applications without having to worry about cross browser issues.
  • We develop single page based applications i.e we develop the application such that the UI is changed in response to user action without page reload. We use AJAX for these.
  • What we do is we make calls to the backend server and request for the dynamic data in json format. You can do these in jQuery using

    //here you pass your entire document to the java script for manipulation
    $(document).ready(function(){
          $('#id_of _ur_element_on_which_you_want_to_perform_ajax_call).getJSON(url,data,
                                                                              function(Data){
    

    //here Data is the response that you have got from the server...

    //suppose you want to get the name from server so what you do is

           $('#id_of_input_field).val(Data.name); 
    
       });  
    

    })

with jQuery you need to do is make calls to the server and update the UI and for all these you just have to use the id of elements. As simple as that and the best part is no issue of cross browser compatibility.

Hope these helps. All the best.

share|improve this answer
    
It did help, thank you! –  user89460 Jun 2 '13 at 8:32
    
Am glad it did. –  svg Jun 3 '13 at 17:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.