我正在尝试使用 javaScript (jquery) / PHP 和 coockies 对表进行动态排序。
mi代码是这样的:
<script type="text/javascript" src="jquery-1.8.2.js"></script>
<script type="text/javascript">
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);
}
}
}
$(document).ready(function(){
$("a").toggle(function(){
setCookie("orden","ASC",100);
}, function() {
setCookie("orden","DESC",100);
});
$("a").click(function(){
setCookie("campo",$(this).html(),100);
$.ajax({
type: "POST",
url: "ordenaTabla.php",
//data: ,
success:function(data) {
//alert(data);
}
});
});
});
</script>
</head>
<body>
<div id="tabla">
<table border="1" align="center" width="300">
<tr>
<td align="center"><a href="#">ID</a></td>
<td align="center"><a href="#">nombre</a></td>
<td align="center"><a href="#">edad</a></td>
</tr>
<?php
include_once ("mySqlConexion.inc.php");
$tabla = "tablaPruebas";
if(isset($_COOKIE["consulta"])){
$consulta = $_COOKIE["consulta"];
$consulta = str_replace("+", " ", $consulta);
}
else{
$consulta = "SELECT * from ".$tabla;
}
echo $consulta;
echo print_r($_COOKIE);
//En la variable resultado se mete el resultado de la ejecución de la consulta que se ejecuta gracias a la funcion mysql_query
$resultado = mysql_query($consulta, $link);
while($fila = mysql_fetch_array($resultado)) {
echo "<tr>";
echo "<td>".$fila["ID"]."</td>";
echo "<td>".$fila["nombre"]."</td>";
echo "<td>".$fila["edad"]."</td>";
echo "</tr>";
};
?>
</table>
</div>
</body>
我在使用 AJAX 调用的 php 文件上使用的代码是这样的:
$tabla = "tablaPruebas";
$consulta = "SELECT+*+FROM+".$tabla."+ORDER+BY+".$_COOKIE["campo"]."+".$_COOKIE["orden"];
//I create this cookie by using the value of the other 2 cookies I already have
$_COOKIE["consulta"] = $consulta;
我想知道你能不能告诉我为什么它不起作用:(
提前致谢...