1

I have checkboxes of categories in a form. When checkbox is changed, from is submitted through ajax and get the values in PHP.

//form

foreach($somethings and $something){
    <input class="input_sale_category" type="checkbox" name="category" value="something->value" />`
}

I get the form data and submit through ajax

      $('.input_sale_category').click(function() {
        var formData = $('#filter_sale_form').serialize();

        jQuery.ajax({
          type: 'GET',
          url: myurl,
          data: formData
          
          success: function(response) {
            console.log(response);
          },
          error: function (request, status, error) {
            alert(request.responseText);
          }
        });
      });

In PHP, I am getting input fields as a string

category=nexus-chair-offer&category=office-desks&category=office-storage

I tried to get the inputs values of category using explode, parse_str but could not get all the values of category

parse_str($str, $output);

var_dump($output);

How can I achieve this? Regex seems an option, but I not good at regex.

VijayRana
  • 953
  • 1
  • 13
  • 38
  • Does this answer your question? [How to pass an array within a query string?](https://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string) – Daniel Protopopov Mar 18 '21 at 06:21
  • if you want it on array form, use `name="category[]"` and then just access it like any normal `$_GET` request in php – Kevin Mar 18 '21 at 07:00

1 Answers1

1

Issue is in you html code, You are using same 'name' property in all of you input tag What this will do is only sand 1 value to backend.

change you input tag like this

foreach($somethings and $something){
<input class="input_sale_category" type="checkbox" name="category[]" value="something->value" />`
}

check this answer as well, it might help, Get checkbox values using checkbox name using jquery

You can also get values manually and pass it in ajax request's data

var checkboxes_value = []; 
   $('.input_sale_category').each(function(){  
        //if($(this).is(":checked")) { 
        if(this.checked) {              
             checkboxes_value.push($(this).val());                                                                               
        }  
   }); 
//checkboxes_value will be array of checked checkboxes

                         
Rudra
  • 704
  • 8
  • 16
  • I already checked by changing the name to array but, ajax submit the same string. – VijayRana Mar 18 '21 at 07:00
  • @VijayRana Can you console fromData and paste it here? https://stackoverflow.com/questions/52441012/how-to-access-and-pass-checkbox-values-to-ajax i also found similar solution here. – Rudra Mar 18 '21 at 07:10
  • Form data is submitted like this `sale_category=725&sale_category=96&sale_category=91` – VijayRana Mar 18 '21 at 07:32
  • I am getting the checkbox values – VijayRana Mar 18 '21 at 07:35
  • @VijayRana Issue here is that you are getting key values pairs but keys should be unique and you have same key 3 times, so php will take only one value. – Rudra Mar 18 '21 at 08:02
  • if you are getting the check box values as array just send that array in request data and you will get your data in php. – Rudra Mar 18 '21 at 08:03