简体   繁体   中英

How to show sql query with checkboxes with ajax and php

i have 3 checkboxes and when i click on it, it should change the sql string and show me a new sql query.

my HTML code:

<div id="#check_article">
<input type="checkbox" value="Iphone">Iphone
<input type="checkbox" value="Samsung">Samsung
<input type="checkbox" value="Nokia">Nokia
</div>

my script:

var arr = [];
$('input[type=checkbox]').change(function () {
        if ($(this).prop("checked")) {
            var value = $(this).val();
            arr.push(value);
        }
        else {
            arr.splice($.inArray($(this).val(),arr),1);
            }
    var new_string = arr.toString();
    console.log(new_string);
});

so when i click on the checkbox my console.log shows me the right value in string. when i uncheck the box it deletes me the value from the string.

now i have to pass it to the server with ajax.

$.ajax({
        url:"query.php",
        method:"POST",
        data: {'qry':new_string},
                success:function(data) {
                ('#result').html(data);
                }
        });

on php side ... i wolud like to search for a checked article with this

$sql = "SELECT * FROM Store WHERE Name LIKE 'mobilePhone' AND Article='....' 
AND Article='....'" AND Article='....'; 

but i realized that i have to format my string. My string is right now : "Iphone,Nokia,Samsung" but i need it to change to AND Article= 'Iphone' AND Article = 'Nokia' AND Article='Samsung'

is this a right way do to it like this or is there a better way ?

Don't Convert it to string

In Front end, remove the toString method

In PHP File you can do like this

 $array = $_POST['qry'];
 $listCheck = "'" . implode("','", $array) . "'";
 echo $sql6 = "SELECT * FROM Store WHERE Name LIKE IN ($listCheck)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM