It always takes me a long time to find an exactly example what I wannt for Jquery AJAX. Here I will collect some example I used frequently.

Submit a form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// this is the id of the form
$("#idForm").submit(function(e) {

    e.preventDefault(); // avoid to execute the actual submit of the form.

    var form = $(this);
    var url = form.attr('action');

    $.ajax({
           type: "POST",
           url: url,
           data: form.serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });


});

Reference

Post (with data)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$.post(
  "surveypost.aspx",
  {
    name: "data",
    name: "data",
    name: "data"
  },
  //if needed
  function(callback) {
      //do something
  }
).done(function() {
    //do something if needed
});

Get