所以我试图创建一个完全ajax 的网站,它仍然允许使用后退按钮和外部链接。我创建了一个 .htaccess 文件,现在我有一个可以在 index.php 文件中处理的 get 数组。问题是我不知道如何使用 PHP 使 Repair.html 替换现有的section.center html 代码。我在其他地方都是用 jQuery 做的,但显然这是 php。只要给我一个正确的方向插入,我知道我已经很接近了!您可以在这里查看该网站:http://techxpertschico.com
这是输出:
Array ( [parameters] => repair )
这是我的 php :我不知道要在评论中添加什么内容。
<?php
if(isset($_GET['parameters']))
{
print_r($_GET);
if($_GET['parameters'] == "repair")
{
// load repair.html into section.center
}
}
?>
这是 jquery : 我通常使用 ajax 请求加载 html 文件。我使用history.js 来管理url 哈希值。
$('.ajaxAnchor').on('click', function (event){
event.preventDefault();
var url = $(this).attr('href');
$.get(url, function(data) {
$('section.center').html(data);
});
var shortened = url.substring(0,url.length - 5);
History.pushState({state:1}, shortened, shortened);
});
从主页加载的section.center示例:
<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>
最佳答案
听起来您只想在查询字符串有匹配的查询字符串时将repair.html 的内容返回给客户端。
在 php 中,你可以通过 include 来完成此操作
if($_GET['parameters'] == "repair")
{
include 'repair.html'
}
关于javascript - 如何使用 PHP 加载 html 文件来替换全 ajax 站点中的部分页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20897916/