i'm trying write json file based upon filled in form
so used external library jquery converts form legitimate json. when console.log output valid json data returned.
so whenever pass data php using $.ajax , write content file php saves file inside says "null"
here ajax:
$(document).ready(function() { var json = $("#user-form").serializejson(); $.ajax({ url: "writejson.php", type: "post", data: json, processdata: false, contenttype: 'application/json' }); })
and here php:
<?php $myfile = "kiosk.json"; $fh = fopen($myfile, 'w') or die("can't open file"); fwrite($fh,var_export($_post['data'], true)); fclose($fh); ?>
and here outputted file says:
null
i tried looking here first , tried numerous options none of them seem save correct data. strange.
thanks in advance!
you change json string this
var json = $("#user-form").serializejson();
to this
var json = {data: $("#user-form").serializejson()};
that way when try retrieve $_post['data'], set because defined it.
edit: important point shuyinsama - pointed out have use json.stringify
method on json objects before posting them php:
in case did:
var jsontext = json.stringify($("#user-form").serializejson()); var json = {data: jsontext};
and use first php file write valid json file.
and remember can use json_decode();
within php if ever need processing on json data, , json_encode();
, respectively.
Comments
Post a Comment