Rumah > pembangunan bahagian belakang > tutorial php > php与html简单的json交互示例

php与html简单的json交互示例

WBOY
Lepaskan: 2016-07-30 13:31:30
asal
1716 orang telah melayarinya


前端(HTML)代码

<code><span><span>html</span>></span><span>head</span>><span>title</span>>前台处理json简单示例<span><span>title</span>></span><span>meta</span><span>charset</span>=<span>"utf-8"</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>script</span>></span><span>script</span><span>type</span>=<span>"text/javascript"</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>body</span>><span>h2</span>>获取用户名和密码<span><span>h2</span>></span>
    用户名:<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"username"</span><span>id</span>=<span>"username"</span>/><span>br</span>/>
    密码:<span>input</span><span>type</span>=<span>"password"</span><span>name</span>=<span>"password"</span><span>id</span>=<span>"password"</span>/><span>br</span>/><span>h2</span>>获取会员信息<span><span>h2</span>></span><span>textarea</span><span>id</span>=<span>"members"</span>><span><span>textarea</span>></span><span><span>body</span>></span><span><span>html</span>></span></code>
Salin selepas log masuk

后台(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></span></code>
Salin selepas log masuk

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了php与html简单的json交互示例,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
gt lt
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan