包含PHP,使用ajax加载页面(无需刷新页面)
我是新来的,所以我想尽可能具体一些 XD 好吧,让我们开始吧:
我用 div+css 创建了一个模板,并用 php 创建了一个脚本,其中包含您从菜单链接调用的文件与“index.php?modul = modulename”这是代码:
<div id="contenuto"><br />
<?php
if(empty($_GET["modul"]) && empty($_GET['userpanel'])) {
require('moduli/news.php');
}
elseif (file_exists("moduli/" . $_GET['modul'] . ".php")) {
include("moduli/" . $_GET['modul'] . ".php");}
elseif (file_exists("moduli/userpanel/" . $_GET['userpanel'] . ".php")) {
include("moduli/userpanel/" . $_GET['userpanel'] . ".php");
}else{
include("moduli/404.php");
}
?>
<br />
</div>
所以...我希望当我单击菜单链接时它将包含特定模块而不刷新页面...抱歉我的英语不好xD
i'm new here so i want to be more specific as possibile XD ok let's start:
i've created a template with div+css,and with php i've created a script That include a file that you call from the menu Links with "index.php?modul=modulename" this is the code:
<div id="contenuto"><br />
<?php
if(empty($_GET["modul"]) && empty($_GET['userpanel'])) {
require('moduli/news.php');
}
elseif (file_exists("moduli/" . $_GET['modul'] . ".php")) {
include("moduli/" . $_GET['modul'] . ".php");}
elseif (file_exists("moduli/userpanel/" . $_GET['userpanel'] . ".php")) {
include("moduli/userpanel/" . $_GET['userpanel'] . ".php");
}else{
include("moduli/404.php");
}
?>
<br />
</div>
So...i would that when i click on a menu link it will include the specific module without refresh the page...sorry for my bad english xD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设这是您的菜单链接列表:
您可以通过一个简单的 jquery 请求和另一个“模块 Bringer”php 文件来实现您所需要的:
详细说明在下面编辑
0 - 好的。首先,你应该让一切在没有 js 和 jquery 的情况下继续工作。机器人不会通过浏览器进行爬行,它们只是获取源代码并爬行代码。 (假设您关心搜索引擎优化)
1 - 为您的菜单链接添加一个额外的属性(在本例中我添加了 rel 属性)。该attr的值是模块参数值。
2 - 包含 jquery 库(如果您之前未包含过 - 您可以从此处下载它。3
-您可以使用我编写的第二个代码部分,您只需更改 jquery 代码中的
$('#menulist li a').click
,当 expample 菜单被触发时。的物品在第一个代码块中,您必须根据您的菜单结构更改此部分,该部分是生成 httprequest 并将结果放入 #contenuto div4 中的部分 - 您需要创建另一个文件来包含将要包含的内容。 jquery httprequest 的目标文件(在本例中我将其命名为 module_bringer.php )。
Let's say this is your menu links list :
You can achieve what you need with a simple jquery request and another "module bringer" php file :
Detailed Explanation Edit Below
0 - Ok. First of all you should make everything keep working without js and jquery. Bots do not crawl via a browser, they just take the source code and crawls through the code. (Assuming that you care for seo)
1 - Make an extra attribute to your menu links (in this case i've added rel attribute). The value of this attr is the module parameter value.
2 - Include jquery library (if you haven't included before - You can download it from here.
3 - You can use the second code part that i've wrote. You just need to change the triggering part. In the jquery code
$('#menulist li a').click
gets triggered when the explample menu items that's in the first code block. You have to change this according to your menu structure. This part is the one that makes the httprequest and puts the results into #contenuto div.4 - You need to create another file for including the content which will be the target file of the jquery httprequest. (in this case i've named it module_bringer.php ). Contents should be like this :
要在不刷新页面的情况下实现新功能,您需要使用 ajax。我建议使用 jQuery 库。
您需要调用 jQuery.ajax 或 加载。这两个函数都将 url 作为第一个参数,可以是 PHP 页面。
To achieve new functionality without a page refresh you will need to use ajax. I recommend using the jQuery library.
You will need to make calls to functions such as jQuery.ajax or load. Both functions take a url as the first argument which can be a PHP page.
您希望链接看起来像
在此示例中,您需要另一个文件
moduli.php
来接收 xmlhttp 请求并包含正确的内容。它可能看起来像这样,这是通过 AJAX 实现的第一种方法,但我不是老手,所以可能有一种更简单的方法来做到这
一点:其他人已经用更简单的方法做出了回应 。使用 jQuery 来完成此操作,所以我建议看一下我的版本的唯一好处是您不必调用来包含该库(尽管这很简单)。
You'll want the links to look like
<a href="#" onClick="getPage(module_name)>link text</a>
. Replacemodule_name
with the module name that you want each link to get. Ajax is all about the javascript `xmlHttpRequest'.In this example you would need another file,
moduli.php
to recieve the xmlhttp request and include the correct file. It might look something like this.That's the first way to do it via AJAX that comes to mind but I'm no veteran so there's probably a much easier way to do it.
Edit: Other people have responded with much simpler ways to accomplish this using jQuery so I recommend taking a look at that. The only benefit to my version is that you wouldn't have to call to include the library (even though it's simple to do).