1internet Posted November 7, 2013 Share Posted November 7, 2013 I have a list of checkboxes, for a filter for manufacturers. I want the checkboxes to update the search results on click, when a new manufacturer is selected or de-selected. The ajax code picks up the checked items from the form like this $('.new_make_button').click(function(){ // when a feature button is selected var serialize = $('#new_make_form').serialize(); // takes all the values of the filter $.ajax({ type : 'POST', url : '../ajax/new_makes.php', // sends the values to ajax file data : serialize, success : function(data) { $("#new_results").html(data); } }); }); I want to add all the selected manufacturer_ids into a list like (1,4,7,10), and set them in a session and run the class, with the updated session. <?php if(isset($_POST['makes'])) $_SESSION['new_results']['makes'] = $_POST['makes']; include '../classes/new-results.php'; $result = new NewResults; $_SESSION['user']['new']; return $result->results(); ?> How can I pass the manufacturer IDs of only the selected manufacturers to the ajax file through the form #new_make_form. How should the html form work to accomplish this? Or do I need to make adjustments in the ajax files? I also am not sure about what the variable for $_POST should be, I have used 'makes' as an example, but not sure how it can catch them all at once. Quote Link to comment https://forums.phpfreaks.com/topic/283677-passing-checkbox-values-through-to-ajax/ Share on other sites More sharing options...
.josh Posted November 7, 2013 Share Posted November 7, 2013 You didn't actually post your form code, so I can't tell you whether or not your javascript looks okay or not. But in general, a request only submits checkboxes that are checked. Assuming your actual ajax request/response works, you can test it by just doing a print_r($_POST); to see the posted vars output. What the format looks like, depends on how you named your checkboxes. One thing I do notice wrong with your php script though is that you need to have a session_start() call before you make use of session vars (and before anything is actually output). Beyond that, was there some other particular issue you are stuck on? I'm afraid there's not really much I can say.. your question is too broad for the code you posted. Quote Link to comment https://forums.phpfreaks.com/topic/283677-passing-checkbox-values-through-to-ajax/#findComment-1457321 Share on other sites More sharing options...
1internet Posted November 7, 2013 Author Share Posted November 7, 2013 The HTML part is what I wasnt sure of Would it be something like this? <input type="checkbox" name="makes[]" value="1"> <input type="checkbox" name="makes[]" value="4"> <input type="checkbox" name="makes[]" value="7"> So it creates an array of makes[], and serializes all the ones that are checked, and passes them through as an array? Can $_POST accept the array? Then I guess I would implode the array? Quote Link to comment https://forums.phpfreaks.com/topic/283677-passing-checkbox-values-through-to-ajax/#findComment-1457336 Share on other sites More sharing options...
.josh Posted November 7, 2013 Share Posted November 7, 2013 You can name your checkboxes whatever you want, but most people do checkboxes like that, yes. Sidenote: a lot of people explicitly set the index as well, because it's fairly common to want to know which checkboxes weren't checked. So it would look something like this: <input type="checkbox" name="makes[0]" value="1"> <input type="checkbox" name="makes[1]" value="4"> <input type="checkbox" name="makes[2]" value="7"> $_POST will be an array of the posted form values, yes. With the format above, if you check all 3 of them, that print_r($_POST); I mentioned earlier will look like this: Array ( [makes] => Array ( [0] => 1 [1] => 4 [2] => 7 ) ) To get them in a comma delimited list, you can do like this: // string value of "1,4,7" $list = implode(',',$_POST['makes']); Quote Link to comment https://forums.phpfreaks.com/topic/283677-passing-checkbox-values-through-to-ajax/#findComment-1457375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.