$.ajax()function with json data from javascript array to a php processing file -
i trying use json first time $.ajax() got values of checkboxes , other needed data php file processing , posting mysqldb through array data section of $.ajax() function empty array[] on php file. when try using javascript debugging tool browser got reprot
**uncaught syntaxerror: unexpected end of input jquery-1.9.0.min.js:1 st.extend.parsejson jquery-1.9.0.min.js:1 (anonymous function) index.php?url=account/registration/:297 st.event.dispatch jquery-1.9.0.min.js:2 y.handle**
the array produced looks @ console log
[checkbox1: "com 101", semester: "1st semester", mid: "7", checkbox2: "com 112", checkbox3: "sta 111"…] checkbox1: "com 101" checkbox2: "com 112" checkbox3: "sta 111" checkbox4: "sta 112" checkbox5: "mth 111" length: 0 mid: "7" semester: "1st semester"
on php processing file did print_r on json data got array[] result
this javascript code block "mydataarray global variable"
$('#mytable2').on('change',function(e){ var rel = e.target.getattribute('rel'); console.log(e.target.value+ " "+ e.target.checked) if(rel === globals.payment_target && e.target.checked===true){ mydataarray[e.target.getattribute("name")]= e.target.value; mydataarray["semester"] = $("#semester").val() mydataarray["mid"] = $("#mid").val() } if(rel === globals.payment_target && e.target.checked ===false){ delete mydataarray[e.target.getattribute("name")] console.log(mydataarray) } }); $('#mytable2').on('click',function(e){ var rel = e.target.getattribute('rel'); console.log(e.target.value+ " "+ e.target.getattribute('name')) if(rel === globals.payment_target && e.target.value =="register"){ console.log(mydataarray) var jsonstring = $.parsejson(mydataarray); var myglob =json.stringify(globals) console.log(myglob) $.ajax({url:'courseregistration.php', type:'post',data:{data:jsonstring},success: function(result){ $('#putmehere').html("<h4 style='text-align:center'>"+result+"</h4>") alert(result) } }) } });
and php file looks like
$data = json_decode($_post['data']); print_r($data);
i can't figure out problem is. can please tell me wrong way i'm doing or suggest better way
try set data type property json
$.ajax({ url:'courseregistration.php', type:'post',data:{data:jsonstring}, datatype:"json", success: function(result){ $('#putmehere').html("<h4 style='text-align:center'>"+result+"</h4>") alert(result) } });
also set content type in php script.
header('content-type: application/json');
Comments
Post a Comment