您的位置:澳门402永利com > 澳门402永利com网络 > PHP+JQUERY操作JSON实例

PHP+JQUERY操作JSON实例

发布时间:2019-09-25 08:06编辑:澳门402永利com网络浏览(84)

    json.html 代码:

    json.html 代码

     代码如下

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>PHP Json传输数据</title>
    </head>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    $(function(){
    $("#submit").click(function(){
    var text = $("input").serialize();
    $.ajax({
    'type':"POST",
    'url':'json_encode.php',
    'dataType':'json',
    'data':text,
    success:insertData
    });
    });
    });

    <html>

    function insertData(data){
    var str = "姓名="+data.name+"<br/>性别="+data.sex+"<br/>年龄="+data.age;
    $("#view").html(str);
    }
    </script>
    <body>
    姓名:<input name="name" id="name" type="text" value=""><br/>
    性别:<input name="sex" id="sex" type="text" value=""><br/>
    年龄:<input name="age" id="age" type="text" value="">
    <input type="submit" name="submit" id="submit" value="提交">
    <div id="view">

    <head>

    </div>
    </body>
    </html>

    <meta http-equiv="Content-Type"content="text/html;charset=utf-8">

    json_encode.php 代码

    <title>PHP Json传输数据</title>

    <?php
    header("Content-type:text/html;charset=utf8");
    include("Json.php");
    $name = $_POST['name'];
    $sex = $_POST['sex'];
    $age = $_POST['age'];
    $json_arg = array('name'=>$name,'sex'=>$sex,'age'=>$age);
    $json = new JSON;
    $json_result = $json->encode($json_arg);
    echo $json_result;
    ?>

    </head>


    <script type="text/javascript"src="js/jquery.js"></script>

    <script type="text/javascript">

    $(function(){

    $("#submit").click(function(){

    vartext = $("input").serialize();

    $.ajax({

    'type':"POST",

    'url':'json_encode.php',

    'dataType':'json',

    'data':text,

    success:insertData

    });

    });

    });

    functioninsertData(data){

    varstr ="姓名="+data.name+"<br/>性别="+data.sex+"<br/>年龄="+data.age;

    $("#view").html(str);

    }

    </script>

    <body>

    姓名:<input name="name"id="name"type="text"value=""><br/>

    性别:<input name="sex"id="sex"type="text"value=""><br/>

    年龄:<input name="age"id="age"type="text"value="">

    <input type="submit"name="submit"id="submit"value="提交">

    <div style="font-size:14px;"id="view">

    </div>

    </body>

    </html>

    json_encode.php 代码

     代码如下

    <?php

    header("Content-type:text/html;charset=utf8");

    include("Json.php");

    $name=$_POST['name'];

    $sex=$_POST['sex'];

    $age=$_POST['age'];

    $json_arg=array('name'=>$name,'sex'=>$sex,'age'=>$age);

    $json=newJSON;

    $json_result=$json->encode($json_arg);

    echo$json_result;

    ?>

    本文由澳门402永利com发布于澳门402永利com网络,转载请注明出处:PHP+JQUERY操作JSON实例

    关键词: