function Ajaxtest () {
$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;
}
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<script type= "Text/javascript" src= "/js/jquery.min.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ ("#send"). Click (function () {
var cont = $ ("input"). Serialize ();
$.ajax ({
URL: "{: url ('/index/index/ajaxtest ')}",
Type: ' Post ',
DataType: ' JSON ',
Data:cont,
Success:function (data) {
var str = data.username + data.age + data.job;
$ ("#result"). html (str);
}
});
});
});
</script>
<body>
<div id= "Result" > a look at the results </div>
<form id= "My" action= "" method= "POST" >
<p><span> Name: </span><input type= "text" name= "username"/></p>
<p><span> Age: </span><input type= "text" Name= "ages"/></p>
<p><span> work: </span><input type= "text" name= "Job"/></p>
</form>
<button id= "Send" > Submit </button>
</body>
One of the simplest examples of Php+jquery+ajax+json