在javascript和php中传递URL参数到页面

Passing URL parameter to a page in javascript and php

本文关键字:参数 URL javascript php      更新时间:2023-09-26

我需要帮助,这就是我实际上想做的。我有两个文件。"index.php"answers"realtime.php" index.php根据url参数"country_code"显示来自特定国家的新闻,realtime.php每2秒更新一次新闻列表。我想要的是real - time.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);
}
});

和real - time.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 } ?>

在我看来,你需要两件事:

    一个mod重写来传递URL变量到你的脚本获取变量的代码片段

这很简单。首先像这样写:

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

在你的real - time.php中你可以使用$_GET['country_code']来使用你的参数