【发布时间】:2015-05-31 14:55:57
【问题描述】:
为了便于理解,我制作了一个示例代码,因为我的实际代码要大得多。
基本上,我想要完成的是运行我的 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>
这是写入 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);
?>
我知道脚本正在运行,所以我很确定问题是 ajax 函数和 php 脚本之间的连接。
谁能帮帮我?
【问题讨论】:
-
在 Ajax 中,您使用函数 sensors.php 但 php 文件称为 sensor.php。或者带有脚本的文件叫做sensors.php?
-
不,不是。 php 文件包括另一个名为 sensor.php 的 php 文件。我知道这可能会让人感到困惑。
-
@user2927356:这是否也回答了您之前的问题stackoverflow.com/q/29278936/367456?