首先在js中定义一个方法 uploadData,代码如下
可以完成数据上传MySql,在表中就可以看到上传的数据了
function uploadData(wifinamePa,addsPa,hottagPa,areaPa,locationtypePa,latPa,lngPa){
//document.getElementById("upload").value = "Processing";
registerRequest = createRequest();
if(registerRequest == null){
alert("Unable to upload the data.");
} else{
var url = "course_collect.php";
//var requestData = "lat=" + latPa +
// "&lon=" + lngPa;
var requestData ="wifi_name=" + wifinamePa +
"&wifi_adds=" + addsPa +
"&hot_tag=" + hottagPa +
"&area=" + areaPa +
"&location_type=" + locationtypePa +
"&lat=" + latPa +
"&lon=" + lngPa;
//registerRequest.onreadystatechange = registrationProcessed;
registerRequest.open("POST",url,true);
registerRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
registerRequest.send(requestData);
}
}
其中函数中的形参就是你要传输的数据,再添加一个发送请求的方法 代码如下
function createRequest(){
try{
request = new XMLHttpRequest();
}catch(tryMS){
try{
request = new ActiveXObject("Msxm12.XMLHTTP");
}catch(otherMS){
try{
request = new ActiveXObject("Microsoft.XMLHTTP");
}catch(failed){
request = null;
}
}
}
return request;
}
然后调用uploadData方法,将实际的参数值传进去即可,接下来定义一个负责接收数据的php文件,代码如下:
<?php
$con = mysql_connect("localhost","root",null);
mysql_query($con, "SET NAMES 'UTF8'");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("a0722152915", $con);
mysql_query("SET NAMES 'UTF8'");
$sql="INSERT INTO access_point (wifi_name,wifi_adds,hot_tag,area,location_type,lat, lon)
VALUES
('$_POST[wifi_name]','$_POST[wifi_adds]','$_POST[hot_tag]','$_POST[area]','$_POST[location_type]','$_POST[lat]','$_POST[lon]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo wifi_name;
mysql_close($con)
?>
可以完成数据上传MySql,在表中就可以看到上传的数据了