这可能是一个简单的问题,尽管我想不出该怎么做。我想加载和修改一个xml文件,然后通过php保存xml。
代码如下:
$.ajax({
type: "GET",
url: "menu.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('menu_item').each(function(){
//change the value of menu_item
$(this).empty();
$(this).text($("textarea").attr("value"));
//send xml to php
$.post('save_xml.php', $(xml), function(data){alert("Data Loaded: " + data);});
}
}
});下面是save_xml.php的外观:
<?php
$xml = $GLOBALS["HTTP_RAW_POST_DATA"];
$file = fopen("file.xml","w");
fwrite($file, $xml);
fclose($file);
echo "ok";
?> 发布于 2011-01-12 17:52:31
这就是你要找的东西吗?
$(this)是您用.each()遍历的每个menu_items
你的代码会变成
$(xml).find('menu_item').each(function(){
$(this).text("New Value");
});希望这能有所帮助
编辑
要将此代码发送回服务器,我将执行以下操作:
$.post('save_xml.php', { xml: $(xml)}, function(data){alert("Data Loaded: " + data);});然后在PHP文件中
<?php
$xml = $_POST['xml'];
$file = fopen("file.xml","w");
fwrite($file, $xml);
fclose($file);
echo "ok";
?> 这段代码是未经测试的,可能有很多原因导致它无法工作,比如文件的写权限等等。
https://stackoverflow.com/questions/4667067
复制相似问题