【问题标题】:get url attribute in AJAX and pass to the PHP在 AJAX 中获取 url 属性并传递给 PHP
【发布时间】:2015-02-13 22:20:44
【问题描述】:

这是我每 5 秒调用一次 boo.php 的 ajax.js。我想从 index.php?color=red 中获取这个参数 color 以在 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

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

【问题讨论】:

  • 问题不清楚。并且代码的格式非常糟糕。括号是常规括号和埃及括号的不一致组合,并且没有缩进。
  • 好吧,让我们变得简单......我怎样才能从 ajax.js 中的 index.php?color=red 获取这个参数颜色 var color =
  • 你刚才不是在那儿回答你的问题吗?
  • 是的,但是我怎样才能将这个 var color 添加到 url:这个 var newurl = url+"?color=?"+; 是否正确
  • 为什么颜色目前在 $_GET 变量中?不,如果该代码在 ajax.js 中,它将无法工作,因为它不会运行 PHP。我认为你需要澄清这个问题。

标签: php jquery ajax post get


【解决方案1】:

这个 getQueryVariable JavaScript 函数应该返回颜色值,所以如果您网站的 url 是 domain.tld?color=red,时间戳值将是红色。

var newurl = url+"?color="+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);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2016-11-17
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    相关资源
    最近更新 更多