1

I have a basic form that has several checkboxes. When a checkbox is checked it will append the value to a url for an ajax call. After I pass these values to mypage.php?item1=1&item2=2&item3=3, I am using GET[‘item’] to retrieve and store these values in order to use in an SQL query. The complexity of this problem is the amount of values passed may vary since it is based on checkboxes. For example, you can pass 1 value or pass all three values. My question is: How can I make this dynamic that would work with the sql query? If more than one value is passed, then the query would require in the WHERE clause an AND for the additional items.

<script>
$('#submitForm').click(function() {
    var url = "'www.mysite.com/mypage.php";
    if ($("#checkbox_form").serialize().length > 0) {
        url += "?" + $("#checkbox_form").serialize();     
    }
    $.getJSON(url)
});
</script>
<form name="checkbox_form" id="checkbox_form">
    <input type="checkbox" value="1" id="item1" name="val1" />
    <input type="checkbox" value="2" id="item2" name="val2" />
    <input type="checkbox" value="3" id="item3" name="val3" />
    <input type="submit" id="submitForm" value="Submit Form" />
<form>

mypage.php?item1=1&item2=2

$item = $_GET['item#']; 

$sql_query = "SELECT info From Products Where :item# AND item#";
$sql_prepare = $db_con->prepare($sql_query);
if (!$sql_prepare->execute(array(':item#' => $item)))
//rest of code
1
  • Your WHERE clause seems wrong. Commented Jan 29, 2014 at 7:47

1 Answer 1

1

First of all, you need to create an array of inputs instead of "val1", "val2".

Just give every input a name of val[x] like val[1] , val[2] and so on. Or you can just let it a new array element: val[].

Then, you need to handle this with a PHP foreach before building the query, something like:

$sql_query = "SELECT info From Products";

$conditions = array();

if (isset($_GET['val']) && is_array($_GET['val'])) {
    $vals = $_GET['val'];
    foreach ($vals as $key => $val) {
        $conditions[] = '`field'. $key .'` = '. $val;
        // You should change the conditions the way you want, I'm not sure about what you want here.
    }
}

if (count($conditions) > 0) {
    $sql_query .= ' where '. implode(' and ', $conditions);
}

And you are done!

UPDATE

As discussed in the comments, this is the condition you are looking for:

    foreach ($vals as $val) {
        $conditions[] = '`item_id` = '. $val;
    }

Moreover, don't forget that you need OR not AND in this context. So, edit this line as well:

    $sql_query .= ' where '. implode(' or ', $conditions);
Sign up to request clarification or add additional context in comments.

14 Comments

Alright, trying this right now. I had the same scenario with a project of mine.
So the name would be like: <input type="checkbox" value="1" id="val[1]" name="val[1]" />?
Exactly. if you don't need the "id" in Javascript, just eliminate it, it won't convert to a Javascript array!
okay passing the values but because i am using [] in the url it looks like this www.mysite.com/mypage.php?val%5B4%5D=1? That shouldnt be a problem right?
haha ok, In my condition i am comparing val[] to item_id and it is showing error: Unknown column 'item_id2' in 'where clause''
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.