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
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
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/
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 ShimanskySource: https://ru.stackoverflow.com/questions/630950/
All Articles