从 js 加载 php 内容


Load php content from js

这是我的javascript代码:

$(document).ready(function(){
    $('#start').click(function(){
        $('#mainbody').html('<?php include "content1.php";?>');
    });
});

而不是加载content1的内容.php在DOM中添加了如下注释:

<div class="row" id="mainbody">
    <!--?php include "content1.php";?-->
</div>

有两种方法可以做到这一点。第一个是,就像我在评论中所说的那样,对正确的内容页面进行 AJAX 调用,检查其响应,如果一切正常,请适当修改 DOM。

另一种方法是我建议你不要做的事情,如果你打算有很多不同的"内容"。我是说您可以将所有内容预加载到单独的隐藏 DOM 元素(例如divs)中,然后修改您必须显示所需元素并隐藏其他元素的单击功能。

当然,选择最佳方法在很大程度上取决于您的应用应该做什么。

$('#mainbody').load('content1.php');

您需要执行 AJAX 调用。

看看这个问题 将数据从 php 传回 ajax

应该是一个很好的例子,帮助你把事情做好。如果您仍然有问题,请向我们展示您的工作进度,我们将很乐意为您提供帮助。

你需要 AJAX 调用

用于入门的示例代码。

// fire off the request to /form.php
    request = $.ajax({
        url: "/form.php",
        type: "post",
        data: serializedData
    });
    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        console.log("Hooray, it worked!");
    });
    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });