php基于jquery的ajax技術(shù)傳遞json數(shù)據(jù)簡單實例
本文實例講述了php基于jquery的ajax技術(shù)傳遞json數(shù)據(jù)簡單實現(xiàn)方法。分享給大家供大家參考,具體如下:
html頁面:
<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">一會看顯示結(jié)果</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頁面:
<?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; ?>
使用post方式
<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 + ")");//轉(zhuǎn)為Object對象 var str = res.username + res.age + res.job; $("#result").html(str); }); }); }); </script>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+ajax技巧與應用小結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- php+xml結(jié)合Ajax實現(xiàn)點贊功能完整實例
- php中XMLHttpRequest(Ajax)不能設(shè)置自定義的Referer的解決方法
- ajax調(diào)用返回php接口返回json數(shù)據(jù)的方法(必看篇)
- ajax處理php返回json數(shù)據(jù)的實例代碼
- PHP 與 js的通信(via ajax,json)
- php+ajax+json 詳解及實例代碼
- jQuery通過ajax請求php遍歷json數(shù)組到table中的代碼(推薦)
- PHP Ajax JavaScript Json獲取天氣信息實現(xiàn)代碼
- PHP封裝返回Ajax字符串和JSON數(shù)組的方法
- Ajax中的JSON格式與php傳輸過程全面解析
- php+Ajax處理xml與json格式數(shù)據(jù)的方法示例
相關(guān)文章
PHP函數(shù)http_build_query使用詳解
這篇文章主要介紹了PHP函數(shù)http_build_query使用詳解,分別對傳入一維關(guān)聯(lián)數(shù)組、一維索引數(shù)組、二維數(shù)組、傳入對象等給出示例,需要的朋友可以參考下2014-08-08PHP保存Base64圖片base64_decode的問題整理
在本篇文章里小編給大家整理的是關(guān)于PHP保存Base64圖片base64_decode的問題,需要的朋友們參考下。2019-11-11