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);



    ?>

    除了数据库,亲测可行

  • 相关阅读:
    UITabar 设置字体大小/颜色
    NSURLSession的基本使用
    报错/警告提示
    实现毛玻璃模糊效果/DRNRealTimeBlur
    免证书真机调试
    xcode添加音效
    NSCalenda日历类
    NSDate--日期格式
    NSArray其中的方法--遍历,
    Mysql学习笔记004
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4383793.html
Copyright © 2011-2022 走看看