0

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>";
  }
4
  • Please specify "build the header again". Subsequent invocations of the sort() function? Reloading the page? Something else? When and how do those invocations occur? Thanks. Commented May 4, 2009 at 7:41
  • ... oh, and I forgot: when and how does the first invocation of sort() occur? Commented May 4, 2009 at 7:44
  • and last but not least: on which browser (and related version) are you running this? Commented May 4, 2009 at 7:54
  • 1
    Why dont you try datatables.net for sorting Commented Mar 20, 2012 at 7:38

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

10 Comments

Thanks for reply Scarlet,the first time is when it is called from main page the index.php when i created the table header the first time index.php <script type="text/javascript"> var sortDir = 'ASC'; var sortBy = 'Time_Date'; function sortDirection() { if (sortDir == 'Desc') { sortDir = 'ASC'; } else { sortDir = 'DESC'; } } </script> <th align="left" onClick="sortDirection(); tableHeader='From'; sort(tableHeader,sortDir);">From:</th> now where should i add the sort direction
And i am running on Mozilla firefox and safari
Hello Sarah, did you already try the changes I suggested? Pls let me know if they are working. If they don't let me define some test cases: a) the page is loaded for the first time; is the function sort() executed? Does it work? b) you click on the 'From' header for the first time; does sort() work? c) you click on the 'From' header for additional 5 times; does sort() work each time? Let's start from here, if we don't come to something this way maybe I think I can't help you w/o looking at the whole code, sorry.
No problem i am a newbie and i could use some help The changes you told me made the error disappear you r other questions the sort is executed once click on From and works fine the first time then the sort direction become undefined
Ok I'd need some more details to help, e.g. with "becomes undefined" do you mean that sortDir as a variable is not recognized anymore or do you refer to the wrong behaviour of the page? Btw, I suggest you check the invocation of your sortDirection() function (e.g. who's calling it and when), because your php code uses sort() directly instead. hth.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.