簡體   English   中英

將URL參數傳遞到javascript和php中的頁面

[英]Passing URL parameter to a page in javascript and php

我需要幫助,這實際上是在嘗試做的事情。 我有兩個文件。 “ index.php”和“ realtime.php” index.php基於url參數“ country_code”顯示來自特定國家/地區的新聞,而realtime.php每2秒更新一次新聞列表。 我想要的是realtime.php以獲取index.php的當前url參數,以便它只能基於url參數更新來自該特定國家/地區的新聞。 我真的需要這個幫助。 再次感謝你。

index.php的腳本

<script type="text/javascript">
$(document).ready(function () {
$.arte({'ajax_url': '../realtime.php?lastid='+$('.postitem:first').attr('id'), 'on_success': update_field, 'time': 1000}).start();
function update_field(data)
{
    $("#realtimeupdate").html(data);
}
});

以及用於realtime.php的腳本

<?php

include"customDB.php";
$lastid = $_REQUEST['lastid'];
$query = 'SELECT count(*) as newpost FROM wp_posts WHERE country_code = "XXXXX" AND    post_id > "'.$lastid.'"';
$result = mysql_query($query);
$rec = mysql_fetch_object($result);
if($rec->newpost){ ?>
<a href="" id="newpostlink">(<?php echo $rec->newpost; ?>) new posts</a>
<script type="text/javascript">document.title = '(<?php echo $rec->newpost; ?>) new  posts'</script>
<?php } ?>

就我所能理解的這個問題而言,您需要兩件事:

  1. Mod重寫以將URL變量傳遞給腳本
  2. 獲取變量的代碼段

所以,那很容易。 首先,您需要編寫如下內容:

RewriteEngine on
RewriteRule ^index\.php?country_code=(.*)$ realtime.php?country_code=$1

並且在您的realtime.php中,您可以使用$_GET['country_code']使用參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM