在AJAX中获取url属性并传递给PHP

时间:2015-02-13 22:20:44

标签: php jquery ajax post get

这是ajax.js,我每隔5秒就会调用一次boo.php。我想从index.php?color = red获取这个参数颜色来处理var中的文件并发送到我的文件boo.php中,这样我就可以获得字段颜色为红色的所有数据。

var seconds = 5;
var divid = "timediv";
var url = "boo.php";

function refreshdiv(){
---
// Timestamp for preventing IE caching the GET request
fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}
var timestamp = fetch_unix_timestamp();
var newurl = url+"?t="+timestamp;
// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout('refreshdiv()',seconds*1000);
}
}
xmlHttp.open("GET",newurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

var seconds;
window.onload = function startrefresh(){
setTimeout('refreshdiv()',seconds*1000);
}

这是我的index.php

<html>
<head>
<script src="ajax.js"></script>
</head>
<body>
<script type="text/javascript"><!--
refreshdiv();
// --></script>
<div id="timediv"></div>    
</body>
</html>

我在这里需要这段代码

var newurl = url+"?t="+timestamp; 

就像boo.php?color = red

非常感谢。我希望这可以做到:(

1 个答案:

答案 0 :(得分:0)

此getQueryVariable JavaScript函数应返回颜色值,因此,如果您网站的网址为domain.tld?color = red,则时间戳值为红色。

var newurl = url +&#34;?color =&#34; + timestamp;

    function getQueryVariable()
    {
        try{
            v = location.search.substring(1).split("&");
            for(var i = 0; i < v.length; i++)
            {
                p = v[i].split("=");
                if(p[0] == "color")
                {
                    if(p[1].indexOf('%20') != -1)
                    {
                        timestamp = decodeURIComponent(p[1]);
                    }
                    else
                    {
                        timestamp = p[1];
                    }
                }
            }
        }
        catch(e)
        {
            console.log(e);
        }
    }