Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Make AJAX REQUEST with clicking in the link not submit button

Tags:

jquery

How could I make an AJAX REQUEST by clicking on a link instead of a submit button? I want once the link is clicked to POST data from input fields

like image 262
choppermio Avatar asked Dec 02 '22 22:12

choppermio


2 Answers

$('selector').click(function(e){
  e.preventDefault();
  $.ajax({
       url: "<where to post>",
       type: "POST",//type of posting the data
       data: <what to post>,
       success: function (data) {
         //what to do in success
       },
       error: function(xhr, ajaxOptions, thrownError){
          //what to do in error
       },
       timeout : 15000//timeout of the ajax call
  });

});
like image 178
Ankur Verma Avatar answered Jan 11 '23 22:01

Ankur Verma


Here's how AJAX works:

$('#link_id').click(function(event){
   event.preventDefault(); // prevent default behavior of link click
   // now make an AJAX request to server_side_file.php by passing some data
   $.post('server_side_file.php', {parameter : some_value}, function(response){
      //now you've got `response` from server, play with it like
      alert(response);
   });
});
like image 32
Vishal Avatar answered Jan 11 '23 21:01

Vishal