Peter Westerlund
Peter Westerlund

Reputation: 749

Convert many GET values to AJAX functionality

I have built a calendar in php. It currently can be controlled by GET values ​​from the URL. Now I want the calendar to be managed and displayed using AJAX instead. So that the page not need to be reloaded.

How do I do this best with AJAX? More specifically, I wonder how I do with all GET values​​? There are quite a few. The only solution I find out is that each link in the calendar must have an onclick-statement to a great many attributes (the GET attributes)? Feels like the wrong way.

Please help me.

Edit: How should this code be changed to work out?

$('a.cal_update').bind("click", function ()
{
event.preventDefault();
update_url = $(this).attr("href");
    $.ajax({
          type      : "GET"
        , dataType  : 'json'
        , url       : update_url
        , async     : false
        , success   : function(data)
                      {
                      $('#calendar').html(data.html);
                      }
    });
return false;
}); 

Upvotes: 0

Views: 247

Answers (2)

Quentin
Quentin

Reputation: 943579

  1. Keep the existing links and forms, build on things that work
  2. You have existing views of the data. Keep the same data but add additional views that provide it in a clean data format (such as JSON) instead of a document format (like HTML). Add a query string parameter or HTTP header that you use to decide which view to return.
  3. Use a library (such as YUI 3, jQuery, etc) to bind event handlers to your existing links and forms to override the normal activation functionality and replace it with an Ajax call to the alternative view.
  4. Use pushState to keep your URLs bookmarkable.

Upvotes: 3

yoda
yoda

Reputation: 10981

You can return a JSON string from the server and handle it with Ajax on the client side.

Upvotes: 0

Related Questions