Sorting using jquery and php
What is wrong with this code, it works the first time and then when I create the header again, an error comes up saying From is undefined
Jquery
function sort(tableHeader,sortDir)
{
$.ajax({
url: "sort.php",
type:"get",
data: "tableHeader="+tableHeader +"&sortdirection="+sortDir,
success:function(data){
$("#t1").html(data);}});}
PHP
$table_Header=$_GET['tableHeader'];
$sort_Dir=$_GET['sortDir'];
if ($table_Header == 'From')
{
$sort_By = 'player_name';
}
else if ($table_Header == 'To')
{
$sort_By = 'player_name';
}
else if ($table_Header== 'Gr')
{
$sort_By = 'grp_abr';
}
if (isset($sort_Dir) && $sort_Dir == 'DESC')
{
$sort_Dir = 'DESC';
}
else
{
$sort_Dir = 'ASC';
}
$str = stripslashes('From');
echo $sortBy;
$result = mysql_query("SELECT * FROM messages,Player
where player_id = from_user
ORDER BY player_name ".$sort_Dir);
echo "<thead>
<tr>
<th style='color:royalblue;'>•</th>
<th align='center'>Time</th>
<th align='left' onClick='sort('From',$sort_Dir);'>De:</th>
<th align='left'>To:</th>
<th align='left'>Gr</th>
</tr>
</thead> ";
while($row = mysql_fetch_array($result))
{
echo "<tbody>
<tr class='highlight'>
<td width='30' align='center' style='color:royalblue'>"."•"."</td>
<td width='70' align='left'>".$row["Time_Date"]."</td>
<td width='600' align='left'>".$row["player_name"]."</td>
<td width='600' align='left'></td>
<td width='100' align='left'></td>
<tr class='highlight'>
<td></td>
<td colspan='4'>".$row["msg_desc"]."</td></tr>
</tbody>";
}
source to share
here, following my observations by looking at the source code.
sort.php:12 >>>
The $ _GET array element for the sort direction is clouded: you name it $_GET["sortDir"]
while the javascript function sort()
is composing an ajax request with this variable in a querystring named sortidirection.
sort.php:28 >>>
You don't need to reverse the sort order here as your javascript function sortDirection()
already does this. So comment lines 28 to 35 and on line 44 do not use the php $sort_Dir
variable but the javascript variable sortDir
.
user.php:48 >>>
the comparison to determine the current sort order is done against a string literal "Desc"
, while you normally use "Desc"
as the value for that variable, so the first try was fine, but the second just does nothing as it "Desc"
differs from "Desc"
in Javascript due to binary string comparison. So change "Desc"
to "Desc"
to your sortDirection()
function.
This should make your stuff work.
source to share
I will first try to change the transition onClick='sort('From',$sort_Dir);'
from single quotes around sort(...)
to double quotes, escaping them with a backslash when you are inside a php string, like this:
onClick=\"sort('From',$sort_Dir);\"
Secondly, inside the same snippet, you are passing $ sort_Dir, which will be translated by php to ASC or DESC. Since in such a line you write some source html code that needs to be inserted somewhere in your document so that the word ASC or DESC appears without quotes around it, the javascript parser will obfuscate.
so you have to change the onClick material like this:
onClick=\"sort('From','$sort_Dir');\"
But as I said in my comment, you should give more details on the first (and only successful, it seems) call to sort () (when it happens, like, etc.).
Try this and see.
source to share