The*_*ude 2 php xml ajax jquery
为了使这个可以理解,我制作了一个示例代码,因为我的实际代码要大得多.
基本上我想要完成的是运行我的PHP脚本,使用ajax编辑XML文件.这是因为我需要在我的真实项目中的javascript中执行此操作.
这是我到目前为止:
包含ajax函数的.php文件:
<!DOCTYPE html>
<html>
<head>
<script>
function editXMLDoc()
{
$.ajax({
url: "sensors.php",
context: document.body
}).done(function() {
$( this ).addClass( "done" );
});
}
</script>
</head>
<body>
<button type="button" onclick="editXMLDoc()">Endre XML</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是写入xml的php脚本:
<?php
include 'sensor.php';
$b=new sensor();
$arr=$b->load('sensor.xml');
for($i=0,$ms=count($arr);$i<$ms;$i++)
{
if($arr[$i]['fields']['status']=='1')
{
$arr[$i]['fields']['status']='0';
}else{
$arr[$i]['fields']['status']='1';
}
}
echo "Completed<br/>";
//3. save array to xml
$b->save('sensor.xml',$arr);
?>
Run Code Online (Sandbox Code Playgroud)
我知道脚本正在运行所以我很确定prob是ajax函数和php脚本之间的连接.
谁能帮我吗?
小智 6
试试这段代码......实际上你没有附加jQuery库.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
var resp = $("#response");
$.ajax({
type: "POST", // Method type GET/POST
url: "sensors.php", //Ajax Action url
data: {},
// Before call ajax you can do activity like please wait message
beforeSend: function(xhr){
resp.html("Please wait...");
},
//Will call if method not exists or any error inside php file
error: function(qXHR, textStatus, errorThrow){
resp.html("There are an error");
},
success: function(data, textStatus, jqXHR){
resp.html(data);
}
});
});
</script>
</head>
<body>
<div id="response"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)