Reputation: 495
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>";
}
Upvotes: 0
Views: 1792
Reputation: 4884
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 javascript sort()
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 function sortDirection()
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 variable sortDir
instead.
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 your sortDirection()
function.
This should get your stuff working.
Upvotes: 2
Reputation: 4884
first I'd try to change the onClick='sort('From',$sort_Dir);'
switching from single quotes around sort(...)
to double quotes, escaping them with a backslash as you're inside a php string there, this way:
onClick=\"sort('From',$sort_Dir);\"
Second, inside the same snippet, you're passing $sort_Dir, which will be translated by php into ASC or DESC. Since in such string you're writing some source html code to be inserted somewhere in your document, to let the word ASC or DESC appear without quotes around it will confuse the javascript parser.
so you should change the onClick stuff like this:
onClick=\"sort('From','$sort_Dir');\"
But as I stated in my comment, you should give more details on the first (and the only successful, it seems) invocation of sort() (when it does occur, how etc.).
Try this and see.
Upvotes: 0