How to submit complex form using jQuery AJAX?

I am trying to submit a form using jQuery ajax. It has several text fields, several flags and a drop-down list of several parameters (you can select several parameters).

Someone here told me that I can get the values ​​of all selected checkboxes using

$("input:checkbox[name=type]:checked")

Then I can look at all the values ​​returned by the above code, assign them to the array as follows:

var types=new Array();

    $.each(cboxes, function()
      {
         types[types.length]=$(this).val();
      }
    );

And try submitting the form using the following command:

var someField=$("#someField").val();
var someField2=$("#someField2").val();
var data={field1 : someField, field2=someField2, s_types:types};
$.post("signup.php?type=p",types);

But this does not work, in particular, flags are not transmitted correctly. How can I make it work?

+3
source share
3 answers

. jQuery . :

$.ajax({
  url: "mybackend.php",
  data: $("#myForm").serialize(),
  success: function(e) { // do something on success },
  error: function(e) { // do something on error }
});

, javascript UTF-8, , , .

+13

. . .

+5

jQuery $. param ( ), $.serialize . , kgiannakis ', $.param, :

function($) {
  $.param = function(a) {
    var s = [];
    if (a.constructor == Array || a.jquery) 
      jQuery.each(a, function() { s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) ); });
    else
      for (var j in a)
        if (a[j] && a[j].constructor == Array) jQuery.each( a[j], function(){ s.push( encodeURIComponent(j) + "[]=" + encodeURIComponent( this ) ); });
        else s.push(encodeURIComponent(j) + "=" + encodeURIComponent(a[j]));
    return s.join("&").replace(/%20/g, "+");
  };
})(jQuery);

... $.serialize, , Danita.

+2

Source: https://habr.com/ru/post/1703888/


All Articles