views:

383

answers:

1

Hello, I am trying to create a dynamic sql query, in which the users clicks a button(link) and the id of that link is sent to the sql query. I would like to accomplish this using jquery. But, being very new to both jquery and PHP, I am finding it very difficult to get this to work properly.

Firstly, I should mention that I am using this php pagination class. I have three links on the main page which hold the ids of the categories that the user can use to filter through the content.

If anyone can help me solve this, that would be great. Thank you.

This is the jquery I started, but like I said, it does not work properly.:

$(document).ready(function(){

    //Display Loading Image
    function Display_Load()
    {
        $("#loading").fadeIn(900,0);
        $("#loading").html("<img src='bigLoader.gif' />");
    }
    //Hide Loading Image
    function Hide_Load()
    {
        $("#loading").fadeOut('slow');
    };

// Sort Content Marketing    
$("a.category").click(function() {
    this_id = $(this).attr("id");
  $.get("pagination3.php", { category: this_id },
    function(data){
      //Load your results into the page
      $("#content").load('pagination3.php?&category='+ this_id );
    });
});



});

Here is what the main page looks like:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="jquery_page.js"></script>

<?php
    //Include the PS_Pagination class
    include('ps_pagination.php');
    //Connect to mysql db
    $conn = mysql_connect('localhost', 'root', 'root');
    mysql_select_db('ajax_demo',$conn);
    $sql = "select * from explore where category='marketing'";
    //Create a PS_Pagination object
    $pager = new PS_Pagination($conn, $sql, 3, 11, 'param1=value1&param2=value2');
    //The paginate() function returns a mysql
    //result set for the current page
    $rs = $pager->paginate();
    //Loop through the result set
    while($row = mysql_fetch_assoc($rs)) {
            echo "<div id='loading'></div>";
            echo "<div id='content'>";

            echo "<table width='800px'>";

                echo "<tr>";
                    echo"<td>";
                    echo $row['id'];
                    echo"</td>";

                    echo"<td>";
                    echo $row['site_description'];
                    echo"</td>";

                    echo"<td>";
                    echo $row['site_price'];
                    echo"</td>";
                echo "</tr>";

            echo "</table>";
            echo "</div>";
    }

        echo "<ul id='pagination'>";

            echo "<li>";
            //Display the navigation
            echo $pager->renderFullNav();
            echo "</li>";

?>

<a href="#" class="category" id="marketing">Marketing</a>

<a href="#" class="category" id="automotive">Automotive</a>

<a href="#" class="category" id="sports">Sports</a>
A: 

One problem is that you have two AJAX calls - get and load. You don't need both.
Either use load:

$("#content").load('pagination3.php?&category='+ this_id );

or get:

$.get("pagination3.php", { category: this_id },
   function(data){
     $("#content").html(data);
   }, "html"
);

(A side note here is that two calls are not necessarily wrong - they might both work. Also, watch out for SQL injection - you are very close..)

Kobi