1

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 !

4
  • Did you do a search on google and here because there are many answers to the same question you're asking might vary but those answer the same thing just need to tweak them to work for you. Commented Oct 10, 2017 at 8:43
  • 1
    You can write ajax on change event of checkbox to get data based on checked value Commented Oct 10, 2017 at 8:43
  • Hi! I searched on google but most results need submitting button, which I don't want. @AshishRana, do you please have a syntax example please ? Commented Oct 10, 2017 at 8:45
  • @Stefey Check below answer Commented Oct 10, 2017 at 8:47

3 Answers 3

1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Hassan, I tried your method but here is the result : select * from site where typeSite in (Array) That's the value of $query. What can I do ?
Hi @Stefey, you should implode the array, so it should be implode($codeSites, ',') so you have a result like this: select * from site where typeSite in(1, 2, 3);
0

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>

2 Comments

Hi Samuel, thanks for your answer. I think maybe that may be good for me, but I don't understand the Ajax syntax (never used). Does it execute a sql query located in the "url" value ? What should contain the "data" variable ?
No it doesn't. It makes a request to your php code and returns result all this in the background without needing to reload or refresh the page. Check this out. sitepoint.com/use-jquerys-ajax-function
0

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;

Comments

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.