[英]What is the fastest way to strip a single GET parameter from a URL in PHP?
[英]A way to get a parameter from the URL
我有这个问题要解决。
我有此页面lista.php
在其中我加载另一个名为pagination.php
页面(每x秒刷新一次)。
我使用以下脚本:
function LoadContent()
{
$('#lista').load('pagination.php');
}
$(document).ready(function(){
LoadContent(); //load contentent when page loads
setInterval('LoadContent()',30000); //schedule refresh
});
嗯,有这个URL:
lista.php?id=3
我需要pagination.php来获取ID,但是它将无法工作.. pagination.php
位于lista.php
...我该如何解决? 我尝试了GET方法。
有什么建议吗? 谢谢。
要从您的网址获取id
(例如lista.php?id=3
您可以使用
var url = window.location.href;
var queryStr = url.substring(url.indexOf("?")+1);
var id = queryStr.split('=')[1];
所以整个代码可能像这样:
function LoadContent()
{
var url = window.location.href;
var queryStr = url.substring(url.indexOf("?")+1);
var id = queryStr.split('=')[1];
$('#lista').load('pagination.php?id=' + id);
}
$(document).ready(function(){
LoadContent(); //load contentent when page loads
setInterval('LoadContent()',30000); //schedule refresh
});
如果您使用AJAX进行动态加载,则该URL是不同的URL,因此请使用location.search.substring(url.indexOf("?"))
类的内容将当前查询字符串附加到请求中。
$('#lista').load('pagination.php' + location.search.substring(url.indexOf("?")));
我认为您可以使用此:
$('#lista').load('pagination.php?id=<?php echo $_GET['id'] ?>');
将参数添加到URL,例如:
function LoadContent()
{
var id = ... <== assign id here
$('#lista').load('pagination.php?id=' + id);
}
$(document).ready(function(){
LoadContent(); //load contentent when page loads
setInterval('LoadContent()',30000); //schedule refresh
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.