夺命雷公狗jquery---63天气查询
2015-11-04 19:29
706 查看
先导入数据,我们可以通过以下PHP代码进行数据导入,
然后用浏览器打开即可导入,我们可以在数据库中看到导入的文件.
下一步就开始我们的HTML代码了,先创建一个index.html的文件
然后又到我们的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控制input不可编辑
- 偷懒的一天-jQuery之事件与应用
- asp.net开发中利用jquery来实现滚动下拉标题浮动固定显示
- js jquery 插件
- jQuery-1.9.1源码分析系列(二)jQuery选择器续1
- 使用JQuery.js插件产生的页面分享带来的不跳转到首页
- jQuery AJAX 请求失败Uncaught ReferenceError: name is not defined
- jQuery 制作美化版的 select 下拉选框
- 【PC端】jQuery+PHP实现浏览更多内容(jquery.more.js插件)
- 【PC端】jQuery+PHP实现浏览更多内容(jquery.more.js插件)
- jQuery官方教程之频繁被问的问题汇总
- 关于jQuery中的$.ajax()使用过程中的一些感悟
- jquery源码分析
- jquery事件绑定
- 最近遇到的若干Web前端问题:disable和readonly,JqueryEasyUI,KindEditor
- 最近遇到的若干Web前端问题:disable和readonly,JqueryEasyUI,KindEditor
- 最近遇到的若干Web前端问题:disable和readonly,JqueryEasyUI,KindEditor
- jQuery全屏滚动插件fullPage.js
- jquery格式化时间戳 2011-01-01
- 介绍Web项目中用到的几款JQuery消息提示插件