1

(editing to make this more clear)

I'm using ajax to create a whole table and initialize jquery.tablesorter. This works fine, except when I try to order by column, all the data disappears from the table.

Code from the ajax file:

 $resultRaw = mysql_query($queryFrame); 
        if($resultRaw and @mysql_num_rows($resultRaw))
        {
            $row_count = mysql_num_rows($resultRaw); 
            $dataTable = 'Count: ' . $row_count . ' <br /><script> $("table").tablesorter({widthFixed: true, widgets: ["zebra"]}).tablesorterPager({container: $("#pager")}); </script> <table cellspacing=".2" class="tablesorter">'; 
            $data = ''; 
            while($rowData = mysql_fetch_assoc($resultRaw))
            {
                $data .= '<tr>'; 
                $headers = array(); 
                foreach($rowData as $header => $dataEntry)
                {
                    $data .= '<td>' . $dataEntry . '</td>'; 
                    //This will get rewritten every turn...
                    $headers[] = $header; 
                }
                $data .= '</tr>'; 
            }
            $dataTable .= '<thead><tr>'; 
            foreach($headers as $singleHeader)
            {
                $dataTable .= '<th>' . $singleHeader . '</th>'; 
            }

            $dataTable .= '</tr></thead><tbody>' . $data . '</tbody></table><div id="pager"></div><script> $("table").trigger("update"); </script> '; 
        }
        else
        {
            echo 'No results returned.'; 
        }
        echo $dataTable;
CookieCoder
  • 349
  • 2
  • 6
  • 2
    Variables don't persist between different pages. You could use session variables. Why don't you go back to the beginning and figure out why your table is being cleared when you try to sort it? That shouldn't happen. – Barmar Dec 27 '13 at 22:43
  • 2
    What do you mean by "show" the URL? What prevents you from checking user role/permissions on every page on the site? Why would you want to post data from a freely open user area of the site to an admin-only area of the site? Why change your approach because you ran into a bug? Basically it just seems to me like you have some bad practices in place with regards to how you have organized the site. – Mike Brant Dec 27 '13 at 22:43
  • @ Barmar From what I could find on jquery.tablesorter it wasn't really meant to be used through ajax. I see where creating the table on page load, and adding data into the tbody through ajax will work. But I need to dynamically create the table headers as well. – CookieCoder Dec 27 '13 at 22:48
  • 1
    I agree with @Barmar you should go back to the ajax method and figure out why it was clearing. What you are proposing now is very insecure/confusing – edhurtig Dec 27 '13 at 22:49
  • @Mike Brant, I'm not going from a non-admin area to an admin area. Its all the admin area. I could recheck permission.. .but I'd rather not do things twice. – CookieCoder Dec 27 '13 at 22:49
  • 1
    @CookieCoder OK. So you are saying that you are only checking permissions when a user somehow "enters" the "admin area"? That seems odd. I would think every single endpoint that would exist in that admin area would need to have a permissions check (i.e. you check against session data to see if logged in use has valid permissions or you redirect to somewhere else). Without such a check, I am guessing you have a very insecure site. – Mike Brant Dec 27 '13 at 22:52
  • @Mike Brant, ok, so permission are checked on many many things throughout the site, on just about every link, in this page, I'm only showing report categories based on different permissions... but this is not the point of the issue. The issue is tablesorter. – CookieCoder Dec 27 '13 at 22:57
  • no reason ajax should be obstacle...depends how you change the table using ajax. If you replace whole table would need to reinitialize plugin – charlietfl Dec 27 '13 at 23:08

1 Answers1

1

Firstly You need to understand the difference between GET and POST method

simply In GET method there is the key value shown in the url

www.example.com/send.php?key=value&Another_key=Another_value

In POST there is key value but it is send after Encoding the data in some other methods

www.example.com/send.php

source: What is the difference between POST and GET?

There is a way to hide the BROWSER URL from the user if you are using jQuery ajax methods.

you can use $.ajax(); to send ajax requests

example:

$.ajax({
   url : 'www.example.com/send.php',
   type: post,    //get as ur need
   data:{
          key  :'value',
          keyn :'valuen'
        },
   success: function(from_url){
      alert(from_url);
   }
});

Ajax: http://api.jquery.com/jQuery.ajax/

Community
  • 1
  • 1
Siv
  • 901
  • 14
  • 26