AJAX is the art of exchanging data with a server and updating parts of a web page without reloading the entire page. This article mainly introduces relevant information about writing example codes for maps through database and ajax methods. Friends in need can refer to
ajax tutorial
AJAX = Asynchronous JavaScript and XML.
AJAX is not a new programming language, but a new way of using existing standards.
AJAX is the art of exchanging data with a server and updating parts of a web page without reloading the entire page.
Client part: html, js, css code part:
Server part: app.js (a JavaScript):
var express=require("express");//引用express var mysql=require("mysql");//引用mysql var app=express();//执行express里的全局函数,返回一个express对象 app.configure(function(){ app.use(app.router);//路由,配置路由时,先执行,用户定义的拦截地址 app.use(express.static(__dirname+"/public"));//设置静态资源路径 app.use(express.errorHandler());//开发者模块,将错误显示在html上 }); app.get("/adress.do",function(req,res){ //console.log("d-----------1"); //建立数据库连接,建立桥梁 var myconn=mysql.createConnection({ host:"localhost", port:"3306", user:"root", password:"123456", database:"pet" }); //打开连接 myconn.connect(); var sql="SELECT * FROM petmap"; //console.log(sql); myconn.query(sql,[],function(err,data){ //console.log(err); //console.log(data); res.send(data); }); //关闭连接 myconn.end(); }); //城市点击响应 app.get("/adressMsg.do",function(req,res){ var pmId=req.query.pmId; console.log(pmId); //建立数据库连接,建立桥梁 var myconn=mysql.createConnection({ host:"localhost", port:"3306", user:"root", password:"123456", database:"pet" }); //打开连接 myconn.connect(); console.log("f------------1"); var sql="SELECT * FROM petmap WHERE pmId=?"; console.log(sql); var id=parseInt(pmId); myconn.query(sql,[id+1],function(err,data){ console.log(err); console.log(data); res.send(data); }); //关闭连接 myconn.end(); }); //监听端口号 app.listen(8888,function(){//监听 console.log("express监听成功!"); console.log(__dirname); });
Database mysql information:
/*创建数据库:pet*/ CREATE DATABASE pet; /*宠物店地图*/ CREATE TABLE petmap(/*宠物店*/ pmId INT AUTO_INCREMENT PRIMARY KEY,/*宠物店id*/ pmName NVARCHAR(60),/*宠物店名*/ pmCity NVARCHAR(20),/*宠物店所在城市*/ pmAddress NVARCHAR(100),/*宠物店所在详细地址*/ pmImg VARCHAR(60),/*宠物店图片*/ pmPhone VARCHAR(30),/*宠物店电话号码*/ pmTop FLOAT,/*宠物店位置上面*/ pmLeft FLOAT/*宠物店位置下面*/ ) /*插入信息*/ INSERT INTO petmap(pmName,pmCity,pmAddress,pmImg,pmPhone,pmTop,pmLeft) VALUES ('邛崃邛临美多宠物服务部','成都','成都市邛崃市长松路296号','map1.png','15202891690',360,320), ('谐和宠物医院','德阳','德阳市旌阳区珠江西路300号','map2.png','0838-6181255',320,350), ('天宁动物医院','西安','西安市新城区韩森路','map3.png','028-81836050',260,240), ('宠美康动物医院','乌鲁木齐','乌鲁木齐市天山区幸福路774号','map4.png','0991-2654158',210,170), ('绵阳康贝动物诊所','绵阳','绵阳市游仙区东津路5-2号','map5.png','0816-2987186',315,335), ('圣心动物医院','重庆','重庆市九龙坡区大公馆九龙大厦3-2','map6.png','023-68820999',360,380), ('吉祥宠物医院(油榨街店)','贵阳','贵阳市南明区油榨街花鸟市场宠物区','map7.png','0851-88275946',400,380), ('常德市武陵区动物医院','常德','常德市武陵区青年路478号','map8.png','0736-7236814',230,393), ('爱尔宠物','郑州','郑州市金水区金水东路3-6号','map9.png','0371-69193157',300,453), ('长沙市博旺宠物诊所','长沙','长沙市天心区西牌楼街41号附近','map10.png','0731-82329801',370,443), ('大嘴狗宠物医院','合肥','合肥市庐阳区北一环与肥西路交口向南','map11.png','0551-64286773',330,500), ('秦皇岛市宠物医院','秦皇岛','秦皇岛市海港区海阳路9号','map12.png','0335-3076769',165,540); INSERT INTO petmap(pmName,pmCity,pmAddress,pmImg,pmPhone,pmTop,pmLeft) VALUES ('乖乖宠宠物医院','天津','天津市河东区万东路77号(近8630医院)','map13.png','13820105131',195,510), ('北京宠物医院','北京','北京市西城区百万庄北里14号','map14.png','010-88377484',198,490), ('爱宠之家宠物医院','哈尔滨','哈尔滨市南岗区鼎新三道街37号','map15.png','0451-82516177',80,625); INSERT INTO petmap(pmName,pmCity,pmAddress,pmImg,pmPhone,pmTop,pmLeft) VALUES ('拉萨妙妙安心宠物诊所','西藏','拉萨市城关区纳金路城东工商1楼','map16.png','0891-6223291',360,170);
Final result:
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Ajax gets data through city name
AJAX request queue implementation
MVC meets ajax form validation after bootstrap
The above is the detailed content of Write example code for the map through database and ajax method. For more information, please follow other related articles on the PHP Chinese website!