views:

29

answers:

1

Hi all,

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 database.

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= "?

+1  A: 

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
ShiVik