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

191
Visualizações
multiple option boxes same name jquery ajax

I've got multiple select boxes that have the same name. Right now when I select the value from the first select box its submitting that one and updating the database. When I select and item on an other select box its submitting the value from the first one. I feel like the javascript isn't right. Any idea what is wrong?

Heres my html:

<?php foreach ($result as $row): ?>
<select class="form-control" name="category[]" required>
    <option value="" disabled selected>Select The Category </option>
    <option value="station">Station</option>
    <option value="equipment">Tools/Equipment</option>
    <option value="supplies">Supplies</option>
</select>
<input id="taskID" value="<?php echo $row['id']; ?>" hidden></input>
<?php endforeach; ?>

jquery:

$(document).on('change', 'select[name="category[]"]', function(event){
  $('select[name="category[]"]').each(function(){
          var formData = {
              'task': $('select[name="category[]"]').val(),
              'name': $('input[name="taskID[]"]').val(),
          };
          $.ajax({
                  type: 'POST',
                  url: 'php/addressBook.php',
                  data: formData,
                  dataType: 'html',
                  encode: true
              })
              .done(function(msg) {
                  $(".alert").html(msg);
              })  
              .fail(function(data) {
                  console.log(data);
              })
          event.preventDefault();
      });
  });

addressBook.php

if (isset($_POST['task'])) {

  $task = $_POST['task'];
  $id = $_POST['name'];

  $stmt = $con->prepare("UPDATE members SET task = ? WHERE id = ?");
  $stmt->bind_param('ss', $task, $id);
  $stmt->execute();

}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Change the class attribute to: class="form-control categorySelect"

$('.categorySelect').change(function(event){
      for(selectInstance of $('.categorySelect')){
          var formData = {
          'task': $(selectInstance).val(),
          'name': $(selectInstance).next().val()
          };


          $.ajax({
              type: 'POST',
              url: 'php/addressBook.php',
              data: formData,
              dataType: 'html',
              encode: true
          })
          .done(function(msg) {
              $(".alert").html(msg);
          })  
          .fail(function(data) {
              console.log(data);
          })
          event.preventDefault();
      }

});
about 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