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.

2
  • Does this answer your question? How to pass an array within a query string? Commented Mar 18, 2021 at 6:21
  • if you want it on array form, use name="category[]" and then just access it like any normal $_GET request in php Commented Mar 18, 2021 at 7:00

1 Answer 1

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

                         
Sign up to request clarification or add additional context in comments.

6 Comments

I already checked by changing the name to array but, ajax submit the same string.
@VijayRana Can you console fromData and paste it here? stackoverflow.com/questions/52441012/… i also found similar solution here.
Form data is submitted like this sale_category=725&sale_category=96&sale_category=91
I am getting the checkbox values
@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.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.