php與html簡單的json互動範例

WBOY
發布: 2016-07-30 13:31:30
原創
1707 人瀏覽過


前端(HTML)程式碼

<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>
登入後複製

後台(PHP)程式碼

<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>
登入後複製

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

以上就介紹了php與html簡單的json互動範例,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
gt lt
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板