如何根据页面名称触发div

时间:2017-05-26 18:46:13

标签: javascript php jquery html css

如何根据页面名称触发div。

我想根据页面名称在同一个div中显示不同的php响应。

我想的是:

<?php

    $pageName = basename($_SERVER['PHP_SELF']);
    if ($pageName == 'index.php') {
        include("http://website/reponse-from-script-1);
    } else if ($pageName == 'index2.php') {
        include("http://website/reponse-from-script-2);
    } else if ($pageName == 'index3.php') {
        include("http://website/reponse-from-script-3);
    }

?>

这是因为所有页面的结构都是:

<html lang="en">
<head>
    <?php  include("includes/head.php");?> 
</head>
<body>
    <?php  include("includes/top");?>

    <div id= content>
        <?php  include("includes/content.php");?>
    </div>

    <?php  include("includes/footer");?>
</body>
</html>   

我想将这个结构用于所有页面,我希望div“content”能够根据页面标题进行更改。

任何帮助将不胜感激。

谢谢

---------更新2 ---------- 这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <?php  include("includes/head.php");?>
</head>
<body>
    <?php  include("includes/scripts.php");?>
    <div id="wrapper">
   <?php  include("includes/navandsidebar.php");?>
   <?php  include("includes/main-menu.php");?>
   <?php  include("includes/secondary-menu.php");?>
   <?php  include("includes/working-space.php");?>

   </div>
</body>
</html>

此代码适用于所有页面,除了这2个部分发生变化,这就是为什么我想在内部触发div。

   <?php  include("includes/secondary-menu.php");?>
   <?php  include("includes/working-space.php");?>

1 个答案:

答案 0 :(得分:1)

实际上,从效率的角度来看,你可以只用一页而不是多页来改变内容div,如下所示:

an_index.php:

 <?php
 include("getContent.php");
 echo "<a href=\"./an_index.php?page=$page\">next</a>";
?>

getContent.php:

<?php
$page = htmlentities($_GET['page']);
if (is_numeric($page)) {

    include("http://localhost/exp/dyn_content.php?page=$page");
    $page++;
    if ($page > 3) {
     $page = 1;
    }

}

dyn_content.php:

<?php
$p = htmlentities($_GET["page"]);
if (is_numeric($p))
{
    switch($p) {
    case 1:
        echo "one<br>\n";
        break;
    case 2:
        echo "two<br>\n";
        break;
    case 3:
        echo "three<br>\n";
        break;
    default:
        echo "one<br>\n";
    break;
    }
}

当然,动态内容脚本可用于从数据库中提取内容。注意:此解决方案依赖于PHP .INI配置,该配置允许allow_url_include = On共享服务器上出现安全问题。