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" i开发者_Python百科d="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";
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
$('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.
精彩评论