Front-end (HTML) code
<code><span><!DOCTYPE html></span><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>title</span>></span>前台处理json简单示例<span></<span>title</span>></span><span><<span>meta</span><span>charset</span>=<span>"utf-8"</span>/></span><span><<span>script</span><span>type</span>=<span>"text/javascript"</span><span>src</span>=<span>"jquery-1.11.3.min.js"</span>></span><span></span><span></<span>script</span>></span><span><<span>script</span><span>type</span>=<span>"text/javascript"</span>></span><span> $(<span>"document"</span>).ready(<span><span>function</span><span>()</span>{</span><span>var</span> url = <span>"server2.php"</span>; <span>var</span> data = {<span>"do"</span>:<span>"first"</span>}; $.getJSON(url,data,<span><span>function</span><span>(res)</span>{</span> $(<span>"#username"</span>).val(res.username); $(<span>"#password"</span>).val(res.password); }); <span>var</span> data = {<span>"do"</span>:<span>"second"</span>}; $.getJSON(url,data,<span><span>function</span><span>(res)</span>{</span> $(<span>"#members"</span>).val(res[<span>2</span>].username); }); }); </span><span></<span>script</span>></span><span></<span>head</span>></span><span><<span>body</span>></span><span><<span>h2</span>></span>获取用户名和密码<span></<span>h2</span>></span> 用户名:<span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"username"</span><span>id</span>=<span>"username"</span>/></span><span><<span>br</span>/></span> 密码:<span><<span>input</span><span>type</span>=<span>"password"</span><span>name</span>=<span>"password"</span><span>id</span>=<span>"password"</span>/></span><span><<span>br</span>/></span><span><<span>h2</span>></span>获取会员信息<span></<span>h2</span>></span><span><<span>textarea</span><span>id</span>=<span>"members"</span>></span><span></<span>textarea</span>></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
Backend (PHP) code
<code><span><span><?php</span><span>// 1.一维</span><span>$member</span>[<span>'username'</span>] = <span>"mike"</span>; <span>$member</span>[<span>'password'</span>] = <span>"123"</span>; <span>// 2.二维</span><span>$members</span>[<span>'1'</span>][<span>'username'</span>] = <span>"mike_1"</span>; <span>$members</span>[<span>'1'</span>][<span>'password'</span>] = <span>"123"</span>; <span>$members</span>[<span>'2'</span>][<span>'username'</span>] = <span>"mike_2"</span>; <span>$members</span>[<span>'2'</span>][<span>'password'</span>] = <span>"123123"</span>; <span>$members</span>[<span>'2'</span>][<span>'address'</span>] = <span>"珠海"</span>; <span>// 根据前台传过来的请求参数来决定执行什么操作</span><span>$do</span> = <span>$_REQUEST</span>[<span>'do'</span>]; <span>switch</span> (<span>$do</span>) { <span>case</span><span>'first'</span>: <span>echo</span> json_encode(<span>$member</span>); <span>break</span>; <span>case</span><span>'second'</span>: <span>echo</span> json_encode(<span>$members</span>); <span>break</span>; } <span>?></span></span></code>
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces a simple json interaction example between php and html, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.