I create an array from checkboxes that are "checked" via JS.
Simple Checkboxes:
<div class="checkbox">
<label>
<input type="checkbox" name="checkSearch[]" value="One" checked /> One
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="checkSearch[]" value="Two" checked /> Two
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="checkSearch[]" value="Three" checked /> Three
</label>
</div>
JS:
var selectedItems = [];
$('input[type="checkbox"]:checked').each(function () {
selectedItems .push($(this).val());
});
//passing array in a function
searchLocations(pass1, pass2, selectedItems);
At this point what is outputted is (assuming all three checkboxes are "checked"):
["One", "Two", "Three"];
Here's where I'm not quite sure what needs to happen next?
Can I include the array as a URL string and use the PHP .implode function later on within my SQL statement?
Here's what I've tried:
function searchLocations(pass1, pass2, selectedItems) {
var searchUrl = 'searchLocations.php?pass1=' + pass1 + '&pass2=' + pass2 + '&selectedItems=' + selectedItems;
...
}
searchLocations.php
$pass1 = $_GET['pass1'];
$pass2 = $_GET['pass2'];
$selectedItems = $_GET['selectedItems'];
$selectedItems = " '".implode("', '", $selectedItems)."' ";
$query = sprintf("SELECT * FROM dbtable WHERE pass1 = $pass1 AND pass2 = $pass2 AND selectedItems IN ($selectedItems)");
$selectedItems needs to read like this in the SQL query: IN ('One', 'Two', 'Three')..
sprintf()sanitizes those$_GETparams$_GET['selectedItems']is not an array. It's a string. Have a look atvar_dump($_GET['selectedItems']);. You might need toexplode()it first. You can also use jQuery to create the query string for you:var searchUrl = 'searchLocations.php?'+$param({pass1: pass1, pass2: pass2, selectedItems: selectedItems});If you do this then$_GET['selectedItems']will be an array and your code should work.sprintf, without any%s? This is a very unsafe piece of code. You should be using prepared statements, or at the very least actually escaping the inputs.