将$ _GET变量传递给javascript函数
问题描述:
我有一个php页面,它显示来自多个MySQL查询的表格,并从它们使用JavaScript函数对列结果进行排序,所有工作正常,我的问题是我需要刷新结果这些查询每隔10秒左右工作正常(使用元刷新),问题是在列排序后进行刷新。当页面刷新排序也被重置。这里是一个排序功能的片段;
<script>
function tablesort(which){ <-----I tried using the $_GET method you suggested
<-----But i get a "missing formal parameter" error
<-----When also using this suggestion and use the
<-----"onclick" i get a "tablesort" is not defined
<-----error
$(document).ready(function(){
if(which == '1.0'){<!--This sorts the pause row, descending -->
$("#Mtable").tablesorter({sortList: [[1,0]]});
}
if(which == '2.1'){<!--This sorts the total dialer row, descending -->
$("#Mtable").tablesorter({sortList: [[2,1]]});
}
if(which == '3.0'){<!--This sorts Wrap-up time row, descending -->
$("#Mtable").tablesorter({sortList: [[3,0]]});
}
if(which == '4.1'){<!--This sorts donation amount row, descending -->
$("#Mtable").tablesorter({sortList: [[4,1]]});
}
if(which == '5.1'){<!--This sorts Up-sale row, descending -->
$("#Mtable").tablesorter({sortList: [[5,1]]});
}
if(which == '6.1'){<!--This sorts the Monthl donation row, descending -->
$("#Mtable").tablesorter({sortList: [[6,1]]});
}
if(which == '7.1'){<!--This sorts the verified sales row, descending -->
$("#Mtable").tablesorter({sortList: [[7,1]]});
}
if(which == '8.1'){<!--This sorts the calles per hour row, descending -->
$("#Mtable").tablesorter({sortList: [[8,1]]});
}
if(which == '9.1'){<!--This sorts the payments per hour row, descending -->
$("#Mtable").tablesorter({sortList: [[9,1]]});
}
if(which == '10.1'){<!--This sorts the average sale row, descending -->
$("#Mtable").tablesorter({sortList: [[10,1]]});
}
if(which == '11.1'){<!--This sorts the sales total row, descending -->
$("#Mtable").tablesorter({sortList: [[11,1]]});
}
});
}
</script>
这里有那种因为刷新IM想从onlcick传递变量数据类似于_GET $因此它的URL的表”
Sort by:
<a onclick="tablesort('1.0')"> Lowest Pause<a/>   
<a onclick="tablesort('2.1')"> Highest Dialer<a/>   
<a onclick="tablesort('3.0')"> Best Wrap-up<a/>   
的链接会像然后读入排序功能;
本地主机/ dbtabke.php?它= 2.1 < ----使用完全相同的网址例如
如何做到这一点任何帮助将不胜感激,谢谢提前。
@prabeen义理我所提供的完整功能,再次感谢
答
最后我做以下使用$ _GET ,这里是代码;
<body>
<!--This gets the element from the URL to set the Sorting, so the page
can be refreshed without losing the sorting-->
<body onload="sorttable.innerSortFunction.apply(document.getElementById('<?php echo $_GET["id"]; ?>'), [])">
<!--table headers that are used to determine columns to sort by-->
echo "<th id=\"dialertime\">".ucfirst("Dial Time")."</th>\n";
echo "<th id=\"pausetime\">".ucfirst("Pause Time")."</th>\n";
...*other table info*
<!--links that sort the specific row-->
<a href="?id=wrap-by-time"> Wrap-up time<a/>   
<a href="?id=wrap-by-percent"> Wrap-up %<a/>   
</body>
感谢每个人的投入,希望它有助于
答
你不一定必须使用GET方法保留排序顺序。
您也可以使用cookie来存储排序顺序。这样您的代码看起来会更清晰。
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
电话时,排序顺序,并通过排序顺序为参数的setCookie()
功能。
当页面首次加载或刷新时,调用相同的排序函数并调用getCookie()
以获取cookie值并在排序前将其设置为排序顺序。
如果你想使用GET,当页面刷新时,这也会做 工作,我希望你在文档准备就绪时调用这个函数tablesort()
。
tablesort('<?php print $_GET['MTable']?>') ;
注:请检查参数是否有效的tablsort()
功能,因为在页面加载时的第一次,GET
变量将是空的,我相信
'$获得(的 'localhost/dbtabke.php?Mtable =' + sort_by_var,...)'? – 2013-04-10 15:38:27
您希望传递查询变量作为通过PHP页面重新载入数据的AJAX请求的一部分。 – 2013-04-10 15:36:42
@Mathew Darnell,我想通过URL将“”tablesort('1.0')“数据传递给javascript函数,因为它的刷新部分 – Iz3k34l 2013-04-10 15:39:07