views:

43

answers:

2

Hello, I am trying to send link id's to a sql query in order to filter data.

When I click a link like the ones below, I want to send the id of the link to the sql query as shown below:

Example: Links (clicked):

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

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

(not clicked):

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

sql query - I hardcoded the id's in, but I want it to do that automatically when someone clicks a link, in this case marketing and automotive, I want the id's to insert automatically using jquery but have no idea how to properly do it:

$query1 = "SELECT * FROM explore WHERE category IN ('marketing', 'automotive',) ORDER BY category LIMIT $start, $limit";
A: 
$('a').click(function(){
var id = $(this).attr('id');
//do something with id
});

I hope you're not relying on javascript to contain an actual SQL query. A better way to do this would be to AJAX the id to some location, and insert the id there, after doing the appropriate scrubbing. Hell, you might just want to bind the view and ajax responder together to operate on the same fixed list of id's. If you receive something not in the pre-approved list (which was presumably used somehow to generate the HTML page), deny the query.

Stefan Kendall
I'm not sure what to do with the id and how to place it in the sql query.
DonDraper
I don't have the rest of your code to make any assumptions, but however you handle the list of clicked items (which would make more sense as a group of checkboxes), you can use the attr method to get the id of whatever has been clicked.
Stefan Kendall
+1  A: 

just do a jquery ajax post with a return false, and capture the result.

$('a').click(function(){
  $.post("querypage.php", { id: "$(this).attr('id')"},
     function(data){
       dosomethingwithdata(data);
  });
 //return false to insure the page doesn't refresh
 return false;
});

Then in your php page (=querypage.php) do:

$query1 = "SELECT * FROM explore WHERE".$_POST["id"]."IN ('marketing', 'automotive',) ORDER BY category LIMIT $start, $limit";

offcourse you now have to execute the query and return the result you want.

I hope this helps, comment me if there is any problem

Nealv
Why is "$(this).attr('id')" in quotes?
enobrev
it gives you the value of the ID. eg. <div id="test"></div>$("this"").attr("id") -> will return 'test'
Nealv