[英]I am trying to change a background image of div using php and jquery.php variable not getting the value
[英]I am trying to change the text in a node in an xml document with jquery and php
我有一个像这样开始的xml文件:
<employee id = "msarapin">
<login>msarapin</login>
<name>Marvin I Sarapin, Ph.D.</name>
<title>Department Head</title>
<position>Administration</position>
我想在单击提交按钮时使用jquery更改“名称”节点内的文本。
这是我的jQuery代码:
$('#submitUpdate').click(function(){
$.ajax({
type: "GET",
url: "db.xml",
dataType: "xml",
success: function(xml)
{
$(xml).find('employee[id$='+"<?php echo($_GET["id"]); ?>"+']').each(function(){
$(this).find('name').each(function(){
$(this).text("New Name");
$.post('saveXml.php', { xml: $(xml)}, function(data){alert("Data Loaded: " + data);});
});
});
}
});
});
这是我的saveXml.php代码:
<?php
$xml = $_POST['xml'];
$file = fopen("db.xml","w");
fwrite($file, $xml);
fclose($file);
echo "ok";?>
我不断收到此错误:
Error: uncaught exception: [Exception... "Could not convert JavaScript argument" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js :: <TOP_LEVEL> :: line 18" data: no]
这里有几个问题,但我敢打赌,绊倒你的是
$.post('saveXml.php', { xml: $(xml) } ... );
看起来您期望$(xml)
以字符串形式回显XML,但实际上它是一个jQuery对象,并且您的错误可能是由于jQuery对象不可序列化。 您需要使用.html()
,这意味着您需要将XML对象包装在另一个节点中,如下所示:
var xmlString = $('<div/>').append(xml).html();
$.post('saveXml.php', { xml: xmlString } ... );
另一个问题是.each()
循环,这是一个样式问题,而不是实际错误。 如果您有多个<employee id="this_id">
节点,或者该节点具有多个<name>
节点,则调用$.post()
次数将比您需要的多。 如果每个节点中只有一个,则不需要嵌套的.each()
方法。 无论哪种方式,您都可以像这样选择:
$(xml)
.find('employee[id$='+"<?php echo($_GET["id"]); ?>"+']')
.find('name')
.text("New Name");
$.post( ... );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.