javascript - How to send JSON to PHP through AJAX? -
i've tried on , on , never works. don't know how receive json on server side.
js
function newreport_compilation(){ ... return json.stringify(formdata); } var formdata = newreport_compilation(); //takes values form ... $("#newreport_submit").on('click',function(){ ajaxrequesttwo("databasebutler.php?reqtype="+2, "text", formdata, function(returneddata){ console.log(returneddata); //to test ... }} function ajaxrequesttwo(reqscript, returndatatype, reqdata, callback){ $.ajax({ type: "post", datatype: returndatatype, url: reqscript, data: {fdata:reqdata}, contenttype: 'application/json; charset=utf-8', success: function(data) { console.log("ajax request success."); callback(data); }, fail: function(){ console.log("ajax request failed."); }, error: function(){ console.log("error!"); } }); } php (databasebutler.php)
function reportinsert(){ def(); //establish new connection $repinscon = new db(); //run insertreport function db class (send report id & json data of submitted form) $result = $repinscon->insertreport($_post['fdata']); echo $result; } if(isset($_get['reqtype'])){ ... }else if($_get['reqtype']=="2"){ reportinsert(); } } deeper php (inside db() class)
public function insertreport($jsonform){ $newrepdata = json_decode($jsonform,true); echo $jsonform; ... //i have sql insert query part commented out here can't here regardless } it gives me error saying notice: undefined index: fdata in c:\wamp\www\hcsprojects\pigeon\main\databasebutler.php
i don't understand how receive json data on php side? json called once reaches php? how receive on server side? :/
i've been looking hours on end. there's no proper answer anywhere.
update
ajax request works fine because 4 , 200 on response. following line in butler.php file problem: (i removed reqtype way.... made url databasebutler.php no additional parameters).
if(isset($_post['req'])){ 'req' in above line 1 json values. console.logged json before sent php. (my json reqdata. console.logged formdata same thing)
{"id":"1615","na":"y","ph":"905-525-9140","em":"h@mcmaster.ca","dep":"residence admissions","req":"pc issues","cus":"","summ":"diwjdiwjdi","det":"","pri":"low","dat":"07/08/2015","tim":"anytime"}
this newreport_compilation function way:
function newreport_compilation(){ var formdata = {id:hash(1),na:$("#newreport_name").val(),ph:$("#newreport_phone").val(),em:$("#newreport_email").val(),dep:$("#newreport_department").val(),req:$("#newreport_requestcategory").val(),cus:$("#newreport_otherrequest").val(),summ:$("#newreport_summary").val(),det:$("#newreport_details").val(),pri:$("input[type='radio'][name='priority']:checked").val(),dat:$("#newreport_date").val(),tim:$("#newreport_time").val()}; return json.stringify(formdata); } how json values on php side? there's no anywhere guys :/
update: found solution
for interested:
problem json.stringify(formdata) in newreport compilation. apparently don't have stringify formdata if stored in json form. tried convert json string json, broke it.
also, when send data in both url , in json form, access using $_request , not $_post or $_get. json data send on goes format send data. in case used post. id or name or server side (databasebutler php) use $_post['id'] or $_post['summ'].
http request can post or not both , code trying access variables through $_post , $_get, , not ok. suggest use $_request instead , take care of (if post or get). despite of using type: "post", jquery take "get" request because pointing url querystring.
Comments
Post a Comment