ajax 如何处理从 javascript 调用的 php 函数中的 echo 语句

发布于 2024-12-01 00:43:13 字数 2493 浏览 0 评论 0原文

我有一个在 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>&nbsp;&nbsp;");', $node

['ID'], $node['ID']);

        printf('$("#%s").append("<a href=\\"#\\" onClick=\\"add_url(\'%s\');\\">Add URL</a>&nbsp;&nbsp;");', $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-12-08 00:43:13

正如 Incognito 所指出的,您的 ajax 调用并不关心您的 PHP 函数正在做什么 - 在这种情况下,重要的是从服务器返回的内容,而不是内容是如何创建的。

尝试将 dataType 选项添加到您的 ajax 调用中以指定 script 响应类型,并使用 appendappendTo 将脚本添加到文档正文的函数:

$.ajax({
    url: "get_nodes.php",
    type: "POST",
    data: { },
    dataType: 'script',
    cache: false,
    success: function (response) {
        if (response != '') 
        {
            $(document.body).append(response);
        }
    }
});

另外,建议:在服务器端函数中,为什么不回显 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 of script, and use the append or appendTo function to add the script to the document body:

$.ajax({
    url: "get_nodes.php",
    type: "POST",
    data: { },
    dataType: 'script',
    cache: false,
    success: function (response) {
        if (response != '') 
        {
            $(document.body).append(response);
        }
    }
});

Also, a suggestion: In your server-side function, why not echo HTML markup instead of a javascript function which creates markup?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文