What is wrong with this code it works the first time then when I build the header again an error occurs 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>";
}
here following my observations, after having a look at your source code.
sort.php:12 >>>the $_GET array element for sort direction is mistyped: you call it$_GET["sortDir"]while the javascriptsort()function composes the ajax request with that variable in querystring called ‘sortdirection’.sort.php:28 >>>You don’t need to reverse sort order here, as your javascript functionsortDirection()already does it. So, comment lines from 28 to 35 and at line 44 do not use the php variable$sort_Dir, but the javascript variablesortDirinstead.user.php:48 >>>the comparison to determine current sort order is made against the string literal"Desc", while generally you use"DESC"as a value for this variable, so the first try was fine but the second simply does nothing, as"DESC"is different from"Desc"in Javascript, due to binary comparison between strings. So change"Desc"to"DESC"into yoursortDirection()function.This should get your stuff working.