将$_GET变量传递给javascript函数
pass $_GET variable to javascript function
我有一个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>
下面是排序表的链接
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/>   
因为刷新,我想把变量数据从onclick传递到URL,类似于$_GET,所以它会像这样,然后读入排序函数;
localhost/dbtab .php?which=2.1 <----确切的URL示例
任何关于如何做到这一点的帮助将是非常感激的,提前感谢。
@prabeen giri我已经提供了完整的功能,再次感谢
不必使用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
变量将为空,我相信
我使用$_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>
感谢大家的意见,希望对大家有所帮助
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 正在全局范围中查找JavaScript函数
- 如何将输入(type=text)从html表单传递到javascript函数
- 如何调用这个匿名 JavaScript 函数
- 通过Ajax将JavaScript函数传递给PHP文件
- 在javascript函数中设置全局变量
- 如何在执行此特定onclick事件时执行JavaScript函数
- 使用javascript函数在页面初始化后加载jquery
- javascript函数同步
- 如何将一个JavaScript函数回调为多个函数
- 在javascript函数中使用php变量
- 代码背后调用一个JavaScript函数的按钮点击-C#
- 将JavaScript函数与HTML分离
- 组合两个javascript函数
- 在 Java 中的 JavaScript 函数中插入 Wicket 值
- javascript函数内部的代码用逗号而不是分号分隔
- href属性内的javascript函数
- 使用html表单中的参数调用JavaScript函数
- 分析Javascript函数中的多个对象
- javascript函数的:和=之间的区别