Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
How to get multi value of checkox with data serialize in ajax jquery?

hey guys I have a problem when I select more than one checkbox the serialize from doesn't put them in an array here is the result

product-category=29&product-category=27

and I want it to be like that

product-category=29,27

this is my php code:

  <form id="myform">
    <fieldset>
      <label>Categorys</label>
      <?php
                $cats = get_terms([
                    'taxonomy' => 'product_cat',
                    'hide_empty' => false,
                ]);
      ?>
        <?php foreach($cats as $cat) : ?>  

          <input type="checkbox" id="product-category-checkbox" name="product-category" value="<?php echo $cat->term_id; ?>">

        <?php endforeach; ?>
   
    </fieldset>
  </form>

and this is the js code:

<script>
  (function($){

    $(document).ready(function(){

      
      $(document).on('submit', '#myform', function(e) {

        e.preventDefault();

        var data = $(this).serialize();

        console.log(data);
        var settings = {
            "url": "<?php echo WC_AJAX::get_endpoint( 'kia_search' ) ?>",
            "method": "POST",
            "data": data
          }
          $.ajax(settings).done(function (response) {
                        $('.products').html(response);
                    
                });

      });
    });
  })(jQuery);
</script>
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Instead of serializing the data directly via .serialize(), you should instead use .serializeArray() to get the data as an array. You can manipulate the array and then serialize it with $.param().

So remove below line, and make it more dynamic

var data = $(this).serialize();

Like so. This is to ensure that, even if more form elements are added later, there will be no need to alter the code.

<script>
    (function($){
        $(document).ready(function(){
            $(document).on('submit', '#myform', function(e) {
                e.preventDefault();

                // get data
                var data = $(this).serializeArray();

                // get 'product-category` values from array and join them
                var categories = data.filter(obj => obj.name === 'product-category').map(obj => obj.value).join(',');

                // add the joined 'product-category' back to data
                if( categories.length > 0 ){
                    data = data.filter(obj => obj.name !== 'product-category')
                               .concat({name: 'product-category', value: categories});
                }

                // serialize data
                data = $.param(data);

                console.log(data);
                var settings = {
                    "url": "<?php echo WC_AJAX::get_endpoint( 'kia_search' ) ?>",
                    "method": "POST",
                    "data": data
                }
                $.ajax(settings).done(function (response) {
                    $('.products').html(response);
                });
            });
        });
    })(jQuery);
</script>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda