Php + ajax + json detailed explanation and instance code, ajaxjson
Php + ajax + json instance code
Html page:
<Html>
Php page:
<?phpheader("Content-type:text/html;charset=utf-8"); $username = $_POST['username']; $age = $_POST['age']; $job = $_POST['job']; $json_arr = array("username"=>$username,"age"=>$age,"job"=>$job); $json_obj = json_encode($json_arr); echo $json_obj;?>
Post method
<Script type = "text/javascript"> $ (function () {$ ("# send "). click (function () {var cont = {username: $ ("input") [0]. value, age: $ ("input") [1]. value, job: $ ("input") [2]. value}; var url = 'AB. php '; $. post (url, cont, function (data) {var res = eval ("(" + data + ")"); // convert it to the Object var str = res. username + res. age + res. job; $ ("# result" cmd.html (str) ;}); </script>
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!