ajax 如何处理从 javascript 调用的 php 函数中的 echo 语句
我有一个在 html 页面末尾调用的脚本,如下所示。
<body>
<script type="text/javascript">
$(document).ready(function() {
//define php info and make ajax call to recreate divs from XML data
$.ajax({
url: "get_nodes.php",
type: "POST",
data: { },
cache: false,
success: function (response) {
if (response != '')
{
alert(response);
}
}
});
});
</script>
</body>
php 脚本有写 javascript 的 echo 语句。 echo 语句不会打印在文档中。他们被送到某个地方,我不知道在哪里。如果我使用警报函数查看 ajax 调用对 php 的响应,所有 echo 语句都会打印在警报框中,就像我希望它们打印在文档本身中一样。
我的问题是:
- ajax 对 php echo 语句做什么?
- 如何将它们打印在文档中?
这是通过ajax调用的PHP文件get_nodes.php。
<?php
function get_nodes() {
// load SimpleXML
$nodes = new SimpleXMLElement('communities.xml', null, true);
foreach($nodes as $node) // loop through
{
echo "\n<script type='text/javascript'>\n";
echo " var newdiv = document.createElement('div');\n";
echo " newdiv.id = '".$node['ID']."';\n";
echo " newdiv.className ='comdiv ui-widget-content';\n";
echo " newdiv.style.top = '".$node->TOP."'\n";
echo " newdiv.style.left = '".$node->LEFT."';\n";
echo " newdiv.style.width = '".$node->WIDTH."';\n";
echo " newdiv.style.height = '".$node->HEIGHT."';\n";
echo " $( '#page' ).append(newdiv);\n";
echo " var heading = document.createElement('p');\n";
echo " heading.innerHTML = '".$node->NAME."';\n";
echo " heading.className ='comhdr editableText ui-widget-header';\n";
echo " $('#".$node['ID']."').append(heading);\n";
echo " $('#".$node['ID']."').resizable();";
echo " $('#".$node['ID']."').draggable();";
echo " $('#".$node['ID']."').draggable('option', 'handle', '.comhdr');";
printf('$("#%s").append("<a href=\\"#\\" onClick=\\"delete_div(\'%s\');\\">Delete</a> ");', $node
['ID'], $node['ID']);
printf('$("#%s").append("<a href=\\"#\\" onClick=\\"add_url(\'%s\');\\">Add URL</a> ");', $node
['ID'], $node['ID']);
echo "\n</script>\n";
}
return;
}
echo get_nodes();
?>
I have a script called at the end of an html page like this.
<body>
<script type="text/javascript">
$(document).ready(function() {
//define php info and make ajax call to recreate divs from XML data
$.ajax({
url: "get_nodes.php",
type: "POST",
data: { },
cache: false,
success: function (response) {
if (response != '')
{
alert(response);
}
}
});
});
</script>
</body>
The php script has echo statements that write javascript. The echo statements are not printed in the document. They are being sent somewhere, I don't know where. If I look at the response from the ajax call to php with the alert function, all the echo statements are printed in the alert box as I would expect them to be printed in the document itself.
My questions are:
- What does ajax do with the php echo statements?
- What is the way to have them printed in the document?
Here is the PHP file get_nodes.php called through ajax.
<?php
function get_nodes() {
// load SimpleXML
$nodes = new SimpleXMLElement('communities.xml', null, true);
foreach($nodes as $node) // loop through
{
echo "\n<script type='text/javascript'>\n";
echo " var newdiv = document.createElement('div');\n";
echo " newdiv.id = '".$node['ID']."';\n";
echo " newdiv.className ='comdiv ui-widget-content';\n";
echo " newdiv.style.top = '".$node->TOP."'\n";
echo " newdiv.style.left = '".$node->LEFT."';\n";
echo " newdiv.style.width = '".$node->WIDTH."';\n";
echo " newdiv.style.height = '".$node->HEIGHT."';\n";
echo " $( '#page' ).append(newdiv);\n";
echo " var heading = document.createElement('p');\n";
echo " heading.innerHTML = '".$node->NAME."';\n";
echo " heading.className ='comhdr editableText ui-widget-header';\n";
echo " $('#".$node['ID']."').append(heading);\n";
echo " $('#".$node['ID']."').resizable();";
echo " $('#".$node['ID']."').draggable();";
echo " $('#".$node['ID']."').draggable('option', 'handle', '.comhdr');";
printf('$("#%s").append("<a href=\\"#\\" onClick=\\"delete_div(\'%s\');\\">Delete</a> ");', $node
['ID'], $node['ID']);
printf('$("#%s").append("<a href=\\"#\\" onClick=\\"add_url(\'%s\');\\">Add URL</a> ");', $node
['ID'], $node['ID']);
echo "\n</script>\n";
}
return;
}
echo get_nodes();
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Incognito 所指出的,您的 ajax 调用并不关心您的 PHP 函数正在做什么 - 在这种情况下,重要的是从服务器返回的内容,而不是内容是如何创建的。
尝试将
dataType
选项添加到您的 ajax 调用中以指定script
响应类型,并使用append
或appendTo
将脚本添加到文档正文的函数:另外,建议:在服务器端函数中,为什么不回显 HTML 标记而不是创建标记的 javascript 函数?
As Incognito pointed out, your ajax call does not care about what your PHP function is doing - all that matters in this case is the content returned from the server, not how the content is created.
Try adding the
dataType
option to your ajax call to specify a response type ofscript
, and use theappend
orappendTo
function to add the script to the document body:Also, a suggestion: In your server-side function, why not echo HTML markup instead of a javascript function which creates markup?