zoukankan      html  css  js  c++  java
  • 夺命雷公狗jquery---63天气查询

    先导入数据,我们可以通过以下PHP代码进行数据导入,

    <?php
        header("Content-Type:text/html;charset=utf-8");
        //1.读取文件到数组
        $file = file('tianqi.txt');
        //print_r($file);
        //2.计算数组长度
        $count = count($file);
        //3.导入数据到数据库
        mysql_connect('localhost','root','');
        mysql_query('use tianqi');
        mysql_query('set names utf8');
    
        //
        for($i=0;$i<$count;$i++){
            $data = explode('=',$file[$i]);
            $code = $data[0];
            $name = $data[1];
    
            //组装sql语句
            $sql = "insert into city values(null,'$code','$name')";
            mysql_query($sql);
        }
    
        mysql_close();

    然后用浏览器打开即可导入,我们可以在数据库中看到导入的文件.

    下一步就开始我们的HTML代码了,先创建一个index.html的文件

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
            <script src="js/jquery.js"></script>
            <script>
                $(function(){
                    //发送ajax请求
                    $.post('city.php',function(msg){
                        $(msg).each(function(i,item){
                            var op = new Option(item.name,item.code);
                            $('#city')[0].options.add(op);
    
                        });
                    },'json');
    
                    //编写ready方法实现页面载入,为按钮绑定实现查询操作
                    $('#btnok').bind('click',function(){
                        var code = $('#city').val();
                        //alert(code);
                        var data = {
                            'code':code
                        }
                        $.post('get.php',data,function(msg){
                            //alert(typeof msg); 查看下返回类型,如果是字符串类型,那么就需要转换下
                            //alert(typeof msg);
                            var json = eval('('+msg+')');
                            //alert(typeof json);
                            var city = json.weatherinfo.city;
                            var wendu = json.weatherinfo.temp1;
                            var qihou = json.weatherinfo.weather;
    
                            $('#result').append(city+'<hr />'+wendu+'<hr />'+qihou)
                        });
                    });
    
                });
            </script>
        </head>
        <body>
            <h1>天气查询</h1>
            <select id="city">
                <option value="-1">请选择城市名称</option>
            </select>
            <hr />
            <input type="button" id="btnok" value="查询" />
            <hr />
            <div id="result"></div>
        </body>
    </html>

    然后又到我们的php代码让接口进行导入了

    <?php
        header("Content-Type:text/html;charset=utf-8");
        $code = $_POST['code'];
        $url = "http://www.weather.com.cn/adat/cityinfo/$code.html";  //这里是一个中国天气网的接口
    
        //模拟发送get请求
        $str = file_get_contents($url);
        echo $str;  //因为本身他就是json文件了,所以步需要转了
  • 相关阅读:
    原生和jQuery的ajax用法
    sublime常用快捷键
    用filter:grayscale将图片过滤成灰色
    Docker搭建Zookeeper集群问题总结
    Linux下jdk环境配置
    window MySQL解压缩版部署及配置
    Windows下Nginx的配置及配置文件部分介绍
    JS 特性:可选链(?.)
    509道Java面试题解析:2020年最新Java面试题
    阿里面试题BIO和NIO数量问题附答案和代码
  • 原文地址:https://www.cnblogs.com/leigood/p/4937024.html
Copyright © 2011-2022 走看看