2013-07-19 23 views
0

我有這個問題,我試圖解決。從URL中獲取參數的一種方法

我有這個頁面lista.php 我在其中加載其他頁面(其中每x秒刷新)命名pagination.php

我使用這個腳本:

function LoadContent() 
{ 
    $('#lista').load('pagination.php'); 
} 
$(document).ready(function(){ 
    LoadContent(); //load contentent when page loads 
    setInterval('LoadContent()',30000); //schedule refresh 
}); 

嗯,有這個網址:

lista.php?id=3 

我需要pagination.php來獲取id,但它不會工作.. the pagination.phplista.php之內......我該如何解決它? 我試過了GET方法..

有什麼建議嗎? 謝謝。

+0

喬治,這是不一樣的帖子.. – Alb

+0

對不起ALB,你說得對,我是太快了/沒有讀正確 – giorgio

回答

0

參數添加到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 
}); 
+0

好吧,這個id是在lista.php中,就像這個lista.php?id = 3,我需要pagination.phph的內容才能得到它,所以我可以在查詢中使用它.. – Alb

0

如果您使用的是AJAX動態加載,那麼這是一個不同的URL,因此將當前查詢字符串附加到請求中,類似於location.search.substring(url.indexOf("?"))

$('#lista').load('pagination.php' + location.search.substring(url.indexOf("?"))); 
+0

請你告訴我如何實現這一點? 我是新的,我從來沒有見過它.. – Alb

0

我想你可以使用這個:

$('#lista').load('pagination.php?id=<?php echo $_GET['id'] ?>'); 
+0

Fairdzs感謝您的幫助,因爲即時新的,請告訴我,我應該在哪裏實施?在哪個頁面?.. – Alb

1

從您的網址像lista.php?id=3獲得id你可以使用

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 
}); 
+0

所以我應該把它插入pagination.php? – Alb

+0

是的,使用bukko的答案代碼。 –