开发者

How to dynamically use checkbox to get data from mysql? - jquery

I am new to jquery and php but I am trying to use a checkbox to grab stuff from a mysql database.

To further explain, I want a checkbox and when checked it will place the id of the checkbox in a mysql query and display the results from the d开发者_StackOverflowatabase.

So, if I check off this box:

<input name="apple" type="checkbox" id="apple" /><label for="apple">Apple</label>

It will send the id or class name (apple) to the php sql query and gather the results.

I am not entirely sure how to do this so if someone can help that would be great thanks.

EDIT

I have a php query that looks like this:

$query1 = "SELECT * FROM explore WHERE category='apple' ORDER BY category";

That query just displays the category with 'apple'

I'm not sure how to go from jquery to php by passing the id's for the query. How do I get the id of the checkbox to go into the php page for the "category= "?


You should be thinking somewhere along the lines of following code...

First you might want to add a class "checked" to your checkboxes like this.

<input name="apple" type="checkbox" id="apple" class="checked" />

Now for the jquery code...

$("input.checked").click(function() {
  $.ajax({
    url: 'ajax/getData.php?id=' + $(this).attr("id"), // the id gets passed here
    success: function(data) {
      alert('Load was performed.'); // Play with your response here
    }
  });
});
  1. On click event of any input with class "checked", this event will be fired.
  2. The id of clicked checkbox will be sent to a server side code, in this case getData.php
  3. You will place the mysql query inside the getData.php, then you will send a formatted response, maybe some xml
  4. Using ajax, the response will be fetched
  5. Then you can manipulate the response as you like
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜