首页 >后端开发 >php教程 >php与html简单的json交互示例

php与html简单的json交互示例

WBOY
WBOY原创
2016-07-30 13:31:301721浏览


前端(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>

后台(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>

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

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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn