javascript - 改变文件中的内容取决于 URL 中的字符串
问题描述
所以我有 3 个文件,即“test1.php”、“test2.php”、“test3.php”。在'index.php'我有这个代码:
<div class="nav_wrapper">
<ul>
<li><a href="#test1">test1</a></li>
<li><a href="#test2">test2</a></li>
<li><a href="#test3">test3</a></li>
</ul>
</div>
<div class="main_content">
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url, 'test1') !== false) {
include 'test1.php';
}
if (strpos($url, 'test2') !== false) {
include 'test2.php';
}
if (strpos($url, 'test3') !== false) {
include 'test3.php';
}
?>
</div>
并且在 test#.php 中只有一个简单<h1>Test #</h1>
的内容,因此我可以查看内容是否正在更改,但它不起作用。
所以我希望每当我点击导航中的链接时,URL 当然会更改为:localhost/index.php#test1; 但内容没有改变?关于如何正确执行此操作的任何想法?JavaScript 或 PHP,谢谢
解决方案
你$_SERVER['REQUEST_URI'];
错了
你可以使用 GET 参数
**或使用 $_SERVER['REQUEST_URI'];
你必须使用字符串函数**
格式如下的 URL >https://IndiaOneWay.com/index.php
或在本地服务器上http://localhost/folderName/1/index.php
$myURL = $_SERVER['REQUEST_URI']
$myURL // >> http://localhost/folderName/*test2.php (trim URL part using str_replace function, replace with nothing "")
$lenthofURL = strlen($myURL);
Get Lenth of Url strlen() [40Charecters] >> find position of "-" stripos("/")[rd position] >> Chop strings left part and store it in variable do the same with remaining
推荐阅读
- service - 拥有一个“蒙面”的 clickhouse-server 是什么意思?
- msbuild - 如何使用来自远程 Swagger Url 的 msbuild 生成 API 客户端?
- android - Android Room 预打包的数据库存在无效架构错误
- java - Apache Ignite 更新先前训练的 ML 模型
- r - 数据“块”上的热图
- visual-studio - Visual Studio 2019 Publish 将警告视为错误
- java - 拆分Linkedlist实现“get方法”
- python - Python 3.75,导入以编辑模式安装的包
- typescript - 如何使用字符串数组作为接口的键?
- excel - 如何构建变量以重用文本设置?