tags:

views:

25

answers:

1

Hello all,

Given so much different options to submit sth to the server, I feel a little confused.

Can someone help me to clear the idea when I should use which and why?

1> $.ajax()
2> $('#myForm').ajaxForm
3> ajaxSubmit
4> $('#myForm').submit

Thank you

+1  A: 

I personally prefer creating a function such as submitForm(url,data) that way it can be reused.

Javascript:

function submitForm(t_url,t_data) {
$.ajax({
  type: 'POST',
  url: t_url,
  data: t_data,
  success: function(data) {
    $('#responseArea').html(data);
  }
});
}

HTML:

<form action='javascript: submitForm("whatever.php",$("#whatevervalue").val());' method='POST'> etc etc

edit try this then:

$('#yourForm').submit(function() {
    var yourValues = {};
    $.each($('#yourForm').serializeArray(), function(i, field) {
        yourValues[field.name] = field.value;
    });
    submitForm('whatever.php',yourvalues);
});
Robert
Hello Robert,My form is very complicated and includes also dynamic fields. If I have to manually transfer all values to the function, it will involve lots of work.thank you
q0987