views:

816

answers:

2

Hello Gurus 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>";
  }

I need urgent help Thanks in advance

A: 

Hello,

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.

Scarlet
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
Sarah
And i am running on Mozilla firefox and safari
Sarah
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.
Scarlet
No problem i am a newbie and i could use some helpThe changes you told me made the error disappear you r other questionsthe sort is executed once click on From and works fine the first time then the sort direction become undefined
Sarah
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.
Scarlet
It is not a problem, maybe I've read too fast the snippet you posted in your comment. Ok if you wish I can take a look at your code. I'll email you, and then we will proceed with this discussion thread, fixing the problem that you're facing.
Scarlet
Ok if you alredy emailed me just confirm here becaue i am gonna have to delete the comment with my email because of the "funny" people.OKBye and thanks
Sarah
Hey Scarlet did you forget about me?
Sarah
Nope, I just had no time, I'm very sorry. BTW within today I'll post my answer. Sorry.
Scarlet
Hey don't be i must have known better(ya you must be busy).Thanks
Sarah
+1  A: 

Hi Sarah,

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.

Scarlet
Thanks Scarlet for your help
Sarah