zoukankan      html  css  js  c++  java
  • 一个php+jquery+json+ajax实例

    json.php

    <!DOCTYPE html Public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <script src="js/jquery-1.7.2.min.js"></script>
    <title>控制台</title>
    <script type="text/javascript">

    $(document).ready(function(){
    // $.ajax({
    // type: "POST",
    // url: "receiveone.php",
    // dataType: "json",
    // data: "id=3",
    // success: function(data){

    // for(var p in data){//遍历json对象的每个key/value对,p为key
    // $("select").append("<option value="+data[p].id+">"+data[p].testname+"</option>");
    // }
    // },
    // error:function(){
    // alert("F");
    // return false;
    // }
    // });
    $.post("receiveone.php",
    {
    id:3,
    },
    function(data,status){
    for(var p in data){//遍历json对象的每个key/value对,p为key
    $("select").append("<option value="+data[p].id+">"+data[p].testname+"</option>");
    }
    },"json");

    });
    </script>
    </head>
    <body>
    <input type="text" />
    <select name="test">
    </select>
    <div></div>
    </body>
    </html>

    receiveone.php

    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("exam", $con);

    // $id=$_POST["id"];
    $result=mysql_query("select * from exam_test where subjectid='3'");
    $i=0;
    while($row = mysql_fetch_array($result))
    {
    $sum[$i]=$row;
    $i++;
    }


    echo json_encode($sum);
    mysql_close($con);



    ?>

    除了数据库,亲测可行

  • 相关阅读:
    远程桌面无法复制粘贴
    软件无法启动,不提示具体错误
    从数据库统计出某张表中某个字段重复次数
    程序员何去何从
    SQL Server中TRUNCATE 和 DELETE的区别
    关于C#自定义控件注释说明
    C#的winform程序下如何实现文本编辑框(TextBox)的Hint提示文字效果
    ubuntu固定内网ip地址
    数据库的优化处理 Memory cached
    MYSQL管理之主从同步管理
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4383793.html
Copyright © 2011-2022 走看看