如何在PHP中更改页面内容?

时间:2016-10-02 14:04:23

标签: php

我有一个包含内容分区的母版页。根据单击的菜单项,我想相应地更改内容。

示例:如果"关于我们"点击,然后我会在内容div中包含关于我们的内容。

1 个答案:

答案 0 :(得分:0)

在没有JS的PHP上。 您需要通过POST或GET方法传递页面标识符

点击页面上的这样一个链接会向名为index.php的母版页发送一个GET请求,其变量'content_id'为'about'值

<a href="index.php?content_id=about">About Us</a>

在PHP脚本中,您需要从Web服务器获取PHP此变量。 喜欢

<?php
    $content=$_GET['content_id']; // _GET is web server array with get variables
?>

在您的母版页的内容部分可能有一个 “if”或“case”构造,其中包含必需的内容

<?php
    if ($content == 'about'){
    echo "About We are the Best";
    // you could include a file or data from database here
}else{
    echo "Some other page";
}
?>

P.S。 PHP有许多已经成熟的模板框架,无需从头开始编写:-)

相关问题