-2

I have a link in a in a table that links to a little tooltip.

<th><a href="#" class="tooltip">Reg NSB$

This works fine, but I'd like the link to resort the table by that column when it's click (a common feature in web tables that I see...) My initial thought was to put an onclick event there, like so:

<th><a href="#" class="tooltip" onclick="<?php mysqli_query($con, "SELECT * FROM report WHERE PMName = '$PMSelection' AND REGNSB <> 0.000 ORDER BY RegNSB DESC Limit $LimitStart,$LimitItems"); ?>">Reg NSB$

I've looked around but I can't figure out a way to accomplish the running of a mysqli query when I click on a "link".

Thanks!

lordterrin
  • 165
  • 2
  • 2
  • 10
  • 6
    You cannot do this. You can run some JavaSctipt that performs an AJAX request to a PHP file that runs a MySQL query. – Jay Blanchard Sep 26 '14 at 18:59
  • -1 As @JayBlanchard said, you cannot do this because HTML/JS run in a completely different environment (client computer) than your PHP/MYSQL (server). – Preston S Sep 26 '14 at 19:30
  • Thank you for this - I will read up on this. I don't know much about the difference between client and server sides. – lordterrin Sep 26 '14 at 21:24
  • The other option is to add query parameters to the link (`href="?sort[]=RegNSB"`), and then re-render the table with the table re-ordered. – forivall Sep 27 '14 at 01:40

1 Answers1

1

You could try the following: Use jQuery to send an AJAX-request to the web-server as soon as a user clicks on that table header:

$(".tooltip").click({function(){
    $.ajax({
            type: "POST",
            url: "server/database.php",  //Set the correct path to the php-file residing on the server here
            data: {
                //Set any data you need to send to the server here
            },
            success : function(data){
                //Called when server responded with success-code
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                //Called when server responded with error-code
            }
        })
    });

Don't forget to wrap this code in the document.ready()-handler of jQuery. Have a look at http://api.jquery.com/jquery.ajax/ for further information about asynchronous calls

enne87
  • 2,221
  • 8
  • 32
  • 61
  • Thank you very much for this. I didn't realize one was client side and one was server side. I'll read up on this and see if I can't make it work. Thanks! – lordterrin Sep 26 '14 at 21:23
  • Phew, this is quite a lot of new stuff. I know nothing about jquery or ajax, but I see the words thrown around a lot. Do you have any other good resources to read up on these? Thanks again! – lordterrin Sep 26 '14 at 21:30
  • Before you start studying jQuery, you have to know that php is a purely server-side scripting language and javascript is totally executeded on the client. The fundamental understanding of the client-server paradigm is important here. With that in mind you could read this tutorial: http://www.w3schools.com/jquery/default.asp – enne87 Sep 27 '14 at 10:02