简体   繁体   English

在PHP页面中使用AJAX加载部分页面

[英]load partial page using AJAX in PHP page

I'm trying to figure out how I can load a partial page (div) into another div using AJAX in my php page. 我试图弄清楚如何在我的php页面中使用AJAX将部分页面(div)加载到另一个div中。

basically I have a div with some php output stuff, and I need to put that div's content into another one using ajax but my code doesn't do anything (it doesn't put the div's content into the other one). 基本上,我有一个带有一些php输出内容的div,并且我需要使用ajax将div的内容放入另一个,但是我的代码不做任何事情(它不会将div的内容放入另一个)。

this is my current code: 这是我当前的代码:

<script type="text/javascript">
$(document).ready(function () {

    function load() {
        $.ajax({
            type: "GET",
            url: "<?php echo $actual_link; ?>",
            dataType: "html",
            success: function(response) {
            //  $("#ajaxContent").html(response);
                $("#issomeone").html($(response).find("#notis"));

                setTimeout(load, 4000);
            }
        });
    }

    load();
});
</script>

so the #notis div holds the php output and the #issomeone div is the div that i need to put the stuff in using ajax. 因此, #notis div保留了php输出,而#issomeone div是我需要将其放入使用Ajax的div中。

is there something missing in my code or I'm just doing it all wrong? 我的代码中是否缺少某些内容,或者我做错了所有事情?

any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

EDIT: 编辑:

THIS DOESN'T DO ANYTHING: 这无能为力:

<script type="text/javascript">
$(document).ready(function () {


function load() {
    $.ajax({
        type: "GET",
        url: "<?php echo $actual_link; ?>",
        dataType: "html",
        success: function(response) {
        //  $("#ajaxContent").html(response);
        //$("#issomeone").html($(response).find("#notis"));


        $('#issomeone').load("<?php echo $actual_link; ?> #notis");

        setTimeout(load, 4000);
    }
});
    }

    load();
});
</script>

You are find ing the DOM element and then trying to set that as target element's innerHTML with html() . 您正在find DOM元素,然后尝试使用html()将其设置为目标元素的innerHTML。 You in essence have a type mismatch. 本质上,您的类型不匹配。

You would need to get the innnerHTML of #notis to pass as the parameter to this function so: 您将需要获取#notis的innnerHTML作为参数传递给此函数,因此:

$("#issomeone").html($(response).find("#notis").html());

Alternately, you could append the node if you want to keep the whole #notis element 或者,如果您想保留整个#notis元素,则可以追加节点

$("#issomeone").append($(response).find("#notis"));

But really, since you are using a straight GET, you might be better off simply doing: 但实际上,由于您使用的是直接GET,因此您最好做一下:

$('#issomeone').load('<?php echo $actual_link; ?> #notis'); 

This provides a more simple abstraction to what you are trying to do more manually with .ajax() 这为您尝试使用.ajax()进行更多手动操作提供了更简单的抽象

I would however encourage you to not get in the habit of loading full HTML pages and then scarping them for some particular element. 但是,我鼓励您不要养成加载完整HTML页面,然后将它们添加到某些特定元素的习惯。 It is better design architecture to have the AJAX target script serve up only content that is needed when possible. 最好的设计架构是让AJAX目标脚本仅在可能时提供所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM