I can not figure out ... Here I get a line, break it in ampersands.

var form = ($('.form').serialize()).split('&'); 

It is necessary to convert to JSON, for transmission over ajax, so that in php after json_decode you can get the array key => value

  • why, if there is php.net/manual/ru/function.parse-url.php - Alexey Shimansky
  • Too long to explain. The integration is done in 1c, the site sends the data to the script in the form of json. It is necessary to recreate the analog js script. - azhirov1991

1 answer 1

 var data = JSON.stringify($( "form" ).serializeArray()); 

shipping:

 $.ajax( { type: "POST", url: 'myHandler.php', data: {json: data}, success: function( response ) { console.log( response ); } }); 

php file

 print_r(json_decode($_POST['json'], true)); 

Note that in this case, you should use serializeArray , which returns an array of objects containing the data of the form elements, not serialize . And then, actually, send a json string


UPD: If not everything works out as it should and you need to do json as key=value , where key is the name of the parameter in the form, and value is the value of the parameter in the form, then you can do this:

 var result = { }; $.each($( "form" ).serializeArray(), function() { result[this.name] = this.value; }); 

that is, to form through the cycle an object from the resulting array and then send it:

 $.ajax( { type: "POST", url: 'get.php', data: {json: JSON.stringify(result)}, success: function( response ) { console.log( response ); } }); 

Addition:

If there are data arrays in the form, a deeper pass is needed .. I will apply an example with html and js right away:

 // функция Π³Π»ΡƒΠ±ΠΎΠΊΠΎΠ³ΠΎ ΠΏΡ€ΠΎΡ…ΠΎΠ΄Π° ΠΈ сСриализации $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }; // ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° $( "form" ).on( "submit", function( event ) { event.preventDefault(); // ΠΏΡ€ΠΈΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ кастомного ΠΌΠ΅Ρ‚ΠΎΠ΄Π° для сСриализации ΠΈ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ Π² json строку var data = $('form').serializeObject(); console.log(data); data = JSON.stringify(data); // ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠ° $.ajax( { type: "POST", url: 'myfile.php', data: {json: data}, success: function( response ) { console.log( response ); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="text" name="firstname" value="122223"><br> <input type="text" name="lastname" value="123"> <input type="text" name="firstname22" value="122223"><br> <input type="text" name="lastname33" value="123"> <input type="text" name="firstnameasd" value="122223"><br> <input type="text" name="lastnameasdasda" value="123"> <input type="text" name="name[]" value="5" /> <input type="text" name="email[]" value="555" /> <input type="text" name="name[]" value="6" /> <input type="text" name="email[]" value="666" /> <input type="submit" /> </form> 

He is on https://jsfiddle.net/3mw6vzww/

  • Yes, it works, thank you very much. Is there a way to get rid of name / value? We need a similarity get - a = 1 & b = 2 ... Where "a" is the key, and "1" value is azhirov1991
  • @ azhirov1991 added in response. most likely you need to use even this way to make an object var result = { }; $.each($( "form" ).serializeArray(), function() { result[this.name] = this.value; }); var result = { }; $.each($( "form" ).serializeArray(), function() { result[this.name] = this.value; }); and then send it as a json string - Alexey Shimansky
  • You are a genius dude, thank you very much!) I just came across an article)) javascript.ru/forum/jquery/56160-serializaciya-form-v-json.html - azhirov1991