前端(HTML)代碼
<html><head><title>前台處理json簡單樣本title><metacharset="utf-8"/><scripttype="text/javascript"src="jquery-1.11.3.min.js">script><scripttype="text/javascript"> $("document").ready(function(){var url = "server2.php"; var data = {"do":"first"}; $.getJSON(url,data,function(res){ $("#username").val(res.username); $("#password").val(res.password); }); var data = {"do":"second"}; $.getJSON(url,data,function(res){ $("#members").val(res[2].username); }); }); script>head><body><h2>擷取使用者名稱和密碼h2> 使用者名稱:<inputtype="text"name="username"id="username"/><br/> 密碼:<inputtype="password"name="password"id="password"/><br/><h2>擷取會員資訊h2><textareaid="members">textarea>body>html>
後台(PHP)代碼
// 1.一維$member['username'] = "mike"; $member['password'] = "123"; // 2.二維$members['1']['username'] = "mike_1"; $members['1']['password'] = "123"; $members['2']['username'] = "mike_2"; $members['2']['password'] = "123123"; $members['2']['address'] = "珠海"; // 根據前台傳過來的請求參數來決定執行什麼操作$do = $_REQUEST['do']; switch ($do) { case'first': echo json_encode($member); break; case'second': echo json_encode($members); break; }?>
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
以上就介紹了php與html簡單的json互動樣本,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。