AJAX基础
<form>
用户名:
<input type="text" id="username" value="" />
密码:
<input type="password" id="password" value="" />
<br />
<br />
<input type="submit" id="update" name="提交" />
</form>
var update = document.getElementById("update");
update.onclick = function () {
var username = document.getElementById("username").value;
var pass = document.getElementById("password").value;
if (window.XMLHttpRequest) {
var ajax = new XMLHttpRequest();
} else {
var ajax = new ActiveXObject("Microsoft.xmlhttp");
}
var url = "http://localhost/ajax/ajax01/dealDate.php?username=" + username + "&password=" + pass;
ajax.open("GET", url, true);
ajax.send();
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
if (ajax.status >= 200 && ajax.status < 300 || ajax.status == 304) {
var p = document.createElement("p");
p.innerHTML = ajax.responseText;
document.body.appendChild(p);
}
}
}
}
<?php
header("Content-type:text/html;charset=utf-8");
$user = $_GET["username"];
$password = $_GET["password"];
echo "{$user}".":"."{$password}";
?>
利用Ajax上传包含图片的数据到sql数据库
<style type="text/css">
#photo {
width: 300px;
height: auto;
display: none;
}
</style>
<form action="" method="post" enctype="multipart/form-data">
真实姓名:
<input type="text" id="username" value="" />
<hr /> 身份证号:
<input type="text" id="id" value="" />
<hr /> 证件照片:
<input type="file" id="file" value="" />
<br />
<img src="" alt="" id="photo" name="img" />
<hr />
<input type="submit" value="验证" id="check" />
</form>
<script type="text/javascript" src="jquery-2.2.4.min.js"></script>
<script type="text/javascript">
var path;
var fileM = document.getElementById("file");
$("#file").on("change", function () {
var formData = new FormData();
formData.append("file", fileM.files[0]);
if (window.XMLHttpRequest) {
var ajax = new XMLHttpRequest();
}else {
var ajax = new ActiveXObject("Microsoft.xmlhttp");
}
ajax.open("POST", "file_up_load.php", true);
ajax.send(formData);
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
if (ajax.status >= 200 && ajax.status < 300 || ajax.status == 304) {
path = ajax.responseText;
$("#photo").css("display","block");
$("#photo").attr("src", ajax.responseText);
}
}
}
});
$("#check").on("click", function () {
var name = document.getElementById("username").value;
var id = document.getElementById("id").value;
$.ajax({
type: "post",
url: "http://localhost/ajax/ajax01/fromAndAjax.php",
data: "username=" + name + "&id=" + id + "&imgpath=" + path
});
})
</script>
<?php
$files = $_FILES["file"];
$types = array("jpg","png","jpeg");
$type = $files["type"];
$imgTypes = explode("/",$type);
if(in_array($imgTypes[1],$types)){
$des = "img/".time().".".$imgTypes[1];
$res = move_uploaded_file($files["tmp_name"],$des);
if ($res) {
echo "http://10.90.93.195:8020/ajax/ajax01/".$des;
}
}
?>
header("Content-type:text/html;charset=utf-8");
$name = $_POST["username"];
$idcard = $_POST["id"];
$imgpath = $_POST["imgpath"];
$mysqli = new mysqli("localhost","root","","H7-44");
$sql = "create table if not exists message(id int unsigned auto_increment primary key,name varchar(50),idcard bigint(18),imgpath varchar(2000))";
$res = $mysqli->query($sql);
$sql = "insert into message(name,idcard,imgpath)value('$name','$idcard','$imgpath')";
$res = $mysqli->query($sql);
ajax-json
<input type="button" name="" id="data" value="获取数据" />
<script type="text/javascript" src="jquery-2.2.4.min.js"></script>
<script type="text/javascript">
$("#data").on("click", function () {
$.ajax({
type: "get",
url: "http://localhost/ajax/ajax02/getData.php",
success: function (txt) {
console.log("txt:" + txt);
var data = JSON.parse(txt);
console.log("data:" + data);
for (var i = 0; i < data.length; i++) {
console.log(data[i].name);
}
}
});
});
</script>
$stu = array(array("name"=>"小菜","sex"=>"男","age"=>30,"sno"=>"10110","classroom"=>"H7班"),array("name"=>"老司机","sex"=>"男","age"=>21,"sno"=>"10111","classroom"=>"H7班"));
$json = json_encode($stu);
echo $json;
JSONP
<input type="button" name="" id="data" value="获取数据" />
<script type="text/javascript" src="jquery-2.2.4.min.js"></script>
<script type="text/javascript">
function getData(txt) {
for (var i = 0; i < txt.length; i++) {
console.log(txt[i].name);
}
}
$("#data").on("click", function () {
var script = $("<script src='http://localhost/ajax/ajax02/JSONP.php?fun=getData'><\/script>");
$("body").append(script);
});
</script>
$fun = $_GET["fun"];
$stu = array(array("name"=>"小菜","sex"=>"男","age"=>30,"sno"=>"10110","classroom"=>"H7班"),array("name"=>"老司机","sex"=>"男","age"=>21,"sno"=>"10111","classroom"=>"H7班"));
$json = json_encode($stu);
echo "{$fun}({$json})";
封装AJAX
用户名:
<input type="text" id="username" value="" />
<br />
<br /> 密码:
<input type="text" id="password" value="" />
<br />
<br />
<input type="submit" id="click" value="提交" />
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript" src="jquery-2.2.4.min.js"></script>
<script type="text/javascript">
var btn = document.getElementById("click");
btn.onclick = function () {
var user = document.getElementById("username").value;
var psd = document.getElementById("password").value;
ajax({
method: "POST",
url: "dealDate.php",
data: {
username: user,
password: psd
},
success: function (test) {
alert(test);
}
});
}
</script>
var ajax = function (obj) {
var method = obj["method"];
if (!method) {
method = "GET";
} var asy = obj.asy;
if (asy == undefined) {
asy = true;
}
if (window.XMLHttpRequest) {
var ajaxR = new XMLHttpRequest();
} else {
var ajxaR = new ActiveXObject("Microsoft.xmlhttp");
}
var url = obj.url;
var data = obj.data;
var array = [];
for (var key in data) {
var value = data[key];
var str = key + "=" + value;
array.push(str);
}
data = array.join("&");
if (method == "GET") {
url = url + "?" + data;
ajaxR.open(method, url, asy);
ajxaR.send();
} else {
ajaxR.open(method, url, asy);
ajaxR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxR.send(data);
} ajaxR.onreadystatechange = function () {
if (ajaxR.readyState == 4) {
if (ajaxR.status >= 200 && ajaxR.status
< 300 || ajaxR.status == 304) {
if (obj.success) {
obj.success(ajaxR.responseText);
}
}
}
}
}
$user = $_POST["username"];
$password = $_POST["password"];
echo "{$user}".":"."{$password}";