Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html form submission via an ajax request? [duplicate]

Possible Duplicate:
jQuery AJAX submit form

I have a form on a page A, and instead of me posting to a new page, is there a way that i can do onSubmit ajax request it instead? I want to make the page to be able to load in the result into a div.

I know how to write the script for the call sort of, but i wasn't sure if i had to hand write it or it if was possible to take all the form information and send it without having to code it all into the attributes. i was thinking I could just do some sort of thing like: onClick, post form but return results into a div.

Thoughts? Ideas?

like image 454
Fallenreaper Avatar asked Jun 29 '12 01:06

Fallenreaper


Video Answer


2 Answers

Try using JQuery's serialize (API ref here)

EDIT: something like this:

 $("input.submit").click( function() {
    $.ajax( {
      type: "POST",
      url: $("form").attr( 'action' ),
      data: $("form").serialize(),
      success: function( response ) {
        //Code to process the response
      }
    });
    return false;
  } );
like image 99
Rob d'Apice Avatar answered Nov 13 '22 21:11

Rob d'Apice


$.ajax({
 url: 'mypage.html',
 success: function(data){
    $('.result').html(data);
 },
 error: function(){
   alert('failure');
 }
});

Then you have a div:

<div class="result"> </div>

This will automatically populate with the result of your ajax post.

http://api.jquery.com/jQuery.post/

Also see TONS of related questions: Jquery checking success of ajax post

like image 25
MultiDev Avatar answered Nov 13 '22 19:11

MultiDev