Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用PHP加载html文件以替换全ajax站点中的部分页面?_Javascript_Php_Jquery_Ajax_.htaccess - Fatal编程技术网

Javascript 如何使用PHP加载html文件以替换全ajax站点中的部分页面?

Javascript 如何使用PHP加载html文件以替换全ajax站点中的部分页面?,javascript,php,jquery,ajax,.htaccess,Javascript,Php,Jquery,Ajax,.htaccess,因此,我试图创建一个完全ajax的站点,它仍然支持使用后退按钮和外部链接。我创建了一个.htaccess文件,现在我有了一个get数组,可以在index.php文件中处理它。问题是我不知道如何使用PHP使repair.html替换现有的section.center html代码。我在jQuery的其他地方都这么做,但显然这是php。只要把我往正确的方向推一推,我知道我就快到了!!您可以在此处查看网站: 这里是输出: Array ( [parameters] => repair ) <

因此,我试图创建一个完全ajax的站点,它仍然支持使用后退按钮和外部链接。我创建了一个.htaccess文件,现在我有了一个get数组,可以在index.php文件中处理它。问题是我不知道如何使用PHP使repair.html替换现有的section.center html代码。我在jQuery的其他地方都这么做,但显然这是php。只要把我往正确的方向推一推,我知道我就快到了!!您可以在此处查看网站:

这里是输出:

Array ( [parameters] => repair )
<section class="center">
            <p>
                Welcome to TechXperts! We offer training and repair for all of your tech devices, but our primary goal is to listen! We know that technology is frustrating so we give you the chance to let us know what your problem is. We will offer the necessary training, or repair to help you have fun using your devices, the way it should be! We feel that is our responsibility to the community as the local TechXperts!
            </p>
            <a href="https://techxpertschico.youcanbook.me/" class="appointmentButton">Book Appointment Now</a>
            <a href="quote.html" class="appointmentButton">Get a Quote</a> 
            <img src="images/random/FrustratedFlyers.png">
            <h3 style="display: inline; position: relative; bottom: 150px;">Friendly staff always available to answer questions!</h3>
            <div style="display: inline; position: relative; left: 310px; bottom: 100px;">
                <img src="/images/random/Apple.png" width="100px">
                <img src="/images/random/Windows8-1.png" height="80px">
                <img src="/images/random/Andoird.png" width="100px">
                <img src="/images/random/check.png" width="100px">
            </div>
        </section>
这是我的php:我不确定该在评论中添加什么内容

<?php
if(isset($_GET['parameters']))
{
    print_r($_GET);
    if($_GET['parameters'] == "repair")
    {
        // load repair.html into section.center
    }
}
?>
主页加载的示例section.center:

Array ( [parameters] => repair )
<section class="center">
            <p>
                Welcome to TechXperts! We offer training and repair for all of your tech devices, but our primary goal is to listen! We know that technology is frustrating so we give you the chance to let us know what your problem is. We will offer the necessary training, or repair to help you have fun using your devices, the way it should be! We feel that is our responsibility to the community as the local TechXperts!
            </p>
            <a href="https://techxpertschico.youcanbook.me/" class="appointmentButton">Book Appointment Now</a>
            <a href="quote.html" class="appointmentButton">Get a Quote</a> 
            <img src="images/random/FrustratedFlyers.png">
            <h3 style="display: inline; position: relative; bottom: 150px;">Friendly staff always available to answer questions!</h3>
            <div style="display: inline; position: relative; left: 310px; bottom: 100px;">
                <img src="/images/random/Apple.png" width="100px">
                <img src="/images/random/Windows8-1.png" height="80px">
                <img src="/images/random/Andoird.png" width="100px">
                <img src="/images/random/check.png" width="100px">
            </div>
        </section>


欢迎来到TechExperts!我们为您的所有技术设备提供培训和维修,但我们的主要目标是倾听!我们知道技术令人沮丧,所以我们给您机会让我们知道您的问题是什么。我们将提供必要的培训或维修,以帮助您以应有的方式享受使用设备的乐趣!作为当地的技术专家,我们觉得这是我们对社区的责任!

友好的员工随时可以回答问题!
Ajax是由浏览器完成的请求,因此不能通过服务器端脚本(PHP)进行。您可以通过php(echo或类似的东西)生成JS代码,并将其发送到浏览器(如果您必须在那里进行ajax调用)。

如果查询字符串具有匹配的查询字符串,听起来您只是想将repair.html的内容返回给客户端

在php中,您可以使用include来实现这一点

if($_GET['parameters'] == "repair")
{
    include 'repair.html'
}

我认为,当您在index.php中获得数组值时,页面已经在加载了。 也许我不需要在这里使用ajax

如果ajax让您的生活更轻松,那么解决方案如下:- 在index.php中添加脚本。当您在参数中获得适当的值时,调用ajax onload of page

<?php
if (isset($_GET['parameters'])) {
    print_r($_GET);
    if ($_GET['parameters'] == "repair") {
        // load repair.html into section.center
        ?>
        <script type="text/javascript">
            $(document).ready(function() {
                var param = '<?php echo $_GET['parameters']; ?>';

                if (param !== "undefined" && param.length > 0) {
                    // ajax call for load repair.html into section.center 
                }
            });
        </script>
        <?php
    }
}
?>

$(文档).ready(函数(){
var参数=“”;
如果(参数!=“未定义”&¶m.length>0){
//ajax调用将repair.html加载到section.center
}
});
替代解决方案:-

为什么不简单地说:

<?php

if (isset($_GET['parameters'])) {
    if ($_GET['parameters'] == "repair") {
        ?>
        <section class="center">
            <p>
                Welcome to TechXperts! We offer training and repair for all of your tech devices, but our primary goal is to listen! We know that technology is frustrating so we give you the chance to let us know what your problem is. We will offer the necessary training, or repair to help you have fun using your devices, the way it should be! We feel that is our responsibility to the community as the local TechXperts!
            </p>
            <a href="https://techxpertschico.youcanbook.me/" class="appointmentButton">Book Appointment Now</a>
            <a href="quote.html" class="appointmentButton">Get a Quote</a> 
            <img src="images/random/FrustratedFlyers.png">
            <h3 style="display: inline; position: relative; bottom: 150px;">Friendly staff always available to answer questions!</h3>
            <div style="display: inline; position: relative; left: 310px; bottom: 100px;">
                <img src="/images/random/Apple.png" width="100px">
                <img src="/images/random/Windows8-1.png" height="80px">
                <img src="/images/random/Andoird.png" width="100px">
                <img src="/images/random/check.png" width="100px">
            </div>
        </section>
        <?php

    }
}
?>


欢迎来到TechExperts!我们为您的所有技术设备提供培训和维修,但我们的主要目标是倾听!我们知道技术令人沮丧,所以我们给您机会让我们知道您的问题是什么。我们将提供必要的培训或维修,以帮助您以应有的方式享受使用设备的乐趣!作为当地的技术专家,我们觉得这是我们对社区的责任!

友好的员工随时可以回答问题!
您知道PHP在服务器上运行,Javascript在客户端浏览器中运行,对吗?我知道,这就是为什么我不知道如何从我的网站设计中解决这个问题的原因。您不能从PHP调用AJAX。你可以做一些有同样效果的事情,但从你的问题中还不清楚这是否必要。你就不能
include()。我编辑了题目。我需要使用repair.html文件更改index.php的section.center。我知道这不会很难,但由于某种原因,我在这方面遇到了困难。也许我的脑子太累了,因为我非常想在一个项目结束时完成这个lol。隧道愿景,我想!那么,您想调用服务器上的一些PHP代码,这些代码将生成并返回一些HTML,然后使用这些HTML覆盖一些现有的HTML?我想用repair.html的内容替换section.center的内容。我可以直接在php中这样做,还是应该像您建议的那样返回整个文件,然后使用jquery替换?因此Bryan,这工作非常出色,除了这样一个事实:即使我将include放在文档树的较低位置,包含的文件的html也始终位于顶部。我如何在页面下方回显它?这是在javascript的客户端处理的。最简单的方法是在你的页面上放置一个div标签,无论你想把内容放在什么地方,这都很奇怪,因为当你导航到repair.html时,我的css正确地处理了我的定位,但是当你把它作为一个链接放进去并尝试去那里时,它会改变方向。我假设它和include有关,因为当我不以这种方式加载它时,它工作得很好。如果你不害怕查看我的随机站点,你可以在这里观察它;)。事实上,我将发布修复代码。htmlnvm已修复。你真棒。我所做的只是用脚本替换static section.center内容,并将逻辑放在其中以包含适当的文件。我知道这很简单。。。就是想不透。非常感谢你给了我一个简单的答案。我太复杂了!因为它不会每次都是相同的代码。那么我的第一个解决方案将对您有效。
var url=$(this.attr('href')中唯一的内容将不适用于您,因为我们正在调用onload of page。你应该在这里写下获取href的确切id。我现在正在实现它!我会告诉你事情的进展。我没想到会如此残忍地模仿剧本。。。但它是有效的!谢谢你的帮助!事实证明#include对我想要的更好,因为我所要做的就是在