Home > Article > Backend Development > A simple example of passing json data using php's ajax technology based on jquery_php skills
The example in this article describes the simple implementation method of PHP's ajax technology based on jquery to transfer json data. Share it with everyone for your reference, the details are as follows:
html page:
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <script type="text/javascript" src="jquery-1.8.2.min.js"></script> <script type="text/javascript"> $(function(){ $("#send").click(function(){ var cont = $("input").serialize(); $.ajax({ url:'ab.php', type:'post', dataType:'json', data:cont, success:function(data){ var str = data.username + data.age + data.job; $("#result").html(str); } }); }); }); </script> </head> <body> <div id="result">一会看显示结果</div> <form id="my" action="" method="post"> <p><span>姓名:</span> <input type="text" name="username" /></p> <p><span>年龄:</span><input type="text" name="age" /></p> <p><span>工作:</span><input type="text" name="job" /></p> </form> <button id="send">提交</button> </body> </html>
php page:
<?php header("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; ?>
Use 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 + ")");//转为Object对象 var str = res.username + res.age + res.job; $("#result").html(str); }); }); }); </script>
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP ajax skills and applications", "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax tutorial", "Summary of PHP office document skills (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary" , "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone in PHP programming.