0

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";

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Why is "$(this).attr('id')" in quotes?
it gives you the value of the ID. eg. <div id="test"></div> $("this"").attr("id") -> will return 'test'
0
$('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.

2 Comments

I'm not sure what to do with the id and how to place it in the sql query.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.