Monday, 15 April 2013

php - submitting several forms with their values by json/ajax, gives me a parseerror -



php - submitting several forms with their values by json/ajax, gives me a parseerror -

on website i've got jquery accordion plugin. in order collect form info (from several forms) i'm using line: $('#form_values').val($('form').serialize()). submit info php file, in case contains:

<?php header('http/1.1 500 internal server booboo'); header('content-type: application/json; charset=utf-8'); homecoming json_encode(array("error" => "bad table name")); ?>

i expect json/ajax/jquery script display alert box "bad table name", instead displays alertbox text: parseerror. jquery script:

$.ajax({ type:'post', data:$('#form_values').val($('form').serialize()), url:'handle.php', datatype: "json", success: function(data){ alert("sucesso: "+data.msg); window.location='index.php'; }, error: function(xmlhttprequest, textstatus, errorthrown){ alert (textstatus);} });

i haven't got clue what's going wrong here.. willing help me out.

you need utilize serializearray instead of serialize , convert json string:

$.ajax({ type:'post', data:json.stringify($('form').serializearray()), url:'handle.php', datatype: "json", success: function(data){ alert("sucesso: "+data.msg); window.location='index.php'; }, error: function(xmlhttprequest, textstatus, errorthrown){ alert (textstatus);} });

php jquery ajax json

No comments:

Post a Comment