Stefey
Stefey

Reputation: 45

PHP & MySQL query depends on checkboxes

I need some help with MySQL, jquery and PHP. Here is my code :

HTML

<label class="checkbox"><input type="checkbox" name="code_site1" class="code_site1" checked="checked">Code 1</label>
<label class="checkbox"><input type="checkbox" name="code_site2" class="code_site2" checked="checked">Code 2</label>
<label class="checkbox"><input type="checkbox" name="code_site3" class="code_site3" checked="checked">Code 3</label>
<label class="checkbox"><input type="checkbox" name="code_site4" class="code_site4" checked="checked">Code 4</label>

Jquery

<script> 
$(document).ready(function(){
    $("#flip").click(function(){
        $("#panel").slideToggle("slow");
    });

});
</script>

Everything here is working. But, I have an SQL query:

SELECT * from site;

I'd like to change it to :

SELECT * from site WHERE codeSite=$theOneThatIsChecked;

Actually, all I want is to establish a sql query depending on what is checked or note. For example, if code_site1 is checked, I'd like to have my SQL query like :

SELECT * from site WHERE codeSite=1;

That's pretty much I'm trying to do but I really don't know how to do it in PHP without submitting...

Can you help me ?

Thanks a lot !

EDIT:

First, thank you for your answsers. I tried the different solutions, but I still have an issue.

Actually, what I do : - On my main page index.php I have an ajax script that points to "do.php" when I click on a submit button, - On the "do.php" page I store a query depending on which checkboxes I have checked, - Finally, thanks to :

success:function(data){
  alert(data);          
}  

I can print the query that I made on "do.php". I'd like to know if I can store it in a php variable on my main page, in order to execute the query on my main page.

Thank you !

Upvotes: 1

Views: 1678

Answers (3)

HSLM
HSLM

Reputation: 2012

so if the request is sent correctly and no problem with that you may change the name of checkboxes to name="code_site[]"

form example:

form should be like this if you want submit

<form method="POST">
 <input type="checkbox" name="code_site[]" value="1">
 <input type="checkbox" name="code_site[]" value="2">
 <input type="checkbox" name="code_site[]" value="3">
 <input type="checkbox" name="code_site[]" value="4">
 <input type="submit">
</form>

with no submit:

$(document).ready(function(){
    $("#flip").click(function(){
        var data = { 'code_site[]' : []};
        $("input:checked").each(function() {
            data['code_site[]'].push($(this).val());
         });
        $.post("do.php", data);
    });

});

then on the query side you can do

and because you are using checkbox so I assume that user can check multiple values, so you need to use "IN"

$codeSites = [];
if($_POST['code_site'])
    $text = $_POST['code_site'];
$query = "select * from codeSite where codeSite in (" . implode($codeSites, ',') . ")";
echo $query; // use it 

sure you need to do some input sanitization

or you can do this:

$codeSites = [];
if($_POST['code_site'])
    $text = $_POST['code_site'];
$query = "select * from codeSite where codeSite in ('" . implode($codeSites, '\',\'') . "')";
echo $query; // use it 

Upvotes: 1

Sean Konig
Sean Konig

Reputation: 134

If I am understanding you correctly you will need to store the post value to a variable and the use that variable in your sql query

Change the input name so that they are all the same and then just add a value

Like this

<label class="checkbox"><input type="checkbox" value="1" name="code_site" class="code_site1" checked="checked">Code 1</label>
<label class="checkbox"><input type="checkbox" value="2" name="code_site" class="code_site2" checked="checked">Code 2</label>

And then something like:

if (isset($_POST['btn-checkbox'])){
$var = $_POST['codesite']
} 

Then your query will be

SELECT * from site WHERE codeSite=$var;

Upvotes: 0

Samuel James
Samuel James

Reputation: 1526

Use Ajax

    <script> 
    $(document).ready(function(){
        $("#flip").click(function(){
            $("#panel").slideToggle("slow");
           if($('input[name="checkbox"]').is(':checked')){
            var checkedValue =$("input[type='checkbox']").val();
          var data ={id: checkedValue};
//make ajax request with the checked value
           
    $.ajax({
       url: url,
       data:data
      
       success: function(response) {
          //process response
       },
       type: 'POST'
    });
        });

    });
}
    </script>

Upvotes: 0

Related Questions