Pass select Multiple values by ajax

Asked

Viewed 415 times

0

I’m trying to pass the selected values in a select Multiple by ajax but I’m not getting it. When executing my code there is no error, but the values arrive empty to the database. The code below is what I tried:

HTML

<select id="admissionais" multiple class="form-control" size="10">
    <option value="cod_01">Código 01</option>
    <option value="cod_02">Código 02</option>
    <option value="cod_03">Código 03</option>
    <option value="cod_04">Código 04</option>
    <option value="cod_05">Código 05</option>
</select>   

Script

$('#salvar').click(function () {                

    admissional = $('#admissionais').val();         

    $.ajax({
      url: "insere_cod.php",
      type: "POST",               
      data: {'admissionais': admissional},            
      success: function(data) {                     
            $('#alert').append('<div class="alert alert-success">O código foi cadastrado com sucesso!</div>').hide().fadeIn().slideUp(3000);
      },
      error: function(){
            $('#alert').append('<div class="alert alert-danger">Não foi possível cadastrar o código!</div>').hide().fadeIn().slideUp(3000);
      }
  });               
});

PHP

$adm = filter_input(INPUT_POST, 'admissionais', FILTER_SANITIZE_STRING);
$sql = "INSERT INTO `tab_cod`(`e_admissionais`) VALUES (:ad)";
$cad_ef = $db->prepare($sql);
$cad_ef->bindParam(':ad', $adm, PDO::PARAM_STR);
$cad_ef->execute();

Note: I want to save the array in the database. Somebody give a hand?

  • You are receiving an 'admissional' array, first receive the value using $_POST['admissional'] and create a for to insert each item.

  • @Wictorchaves I want to save in array format even (1,2,3,5...).

  • But in php you’re not treating it as an array, I’ll give you an answer so you understand what I’m saying.

1 answer

0


To send the string concatenated with comma just make a small change in your javascrip:

$('#salvar').click(function () {                

    admissional = $('#admissionais').val();         

    $.ajax({
        url: "insere_cod.php",
        type: "POST",               
        data: {'admissionais': admissional.join(',')},            
        success: function(data) {                     
            $('#alert').append('<div class="alert alert-success">O código foi cadastrado com sucesso!</div>').hide().fadeIn().slideUp(3000);
        },
        error: function(){
            $('#alert').append('<div class="alert alert-danger">Não foi possível cadastrar o código!</div>').hide().fadeIn().slideUp(3000);
        }
    });               
});

I just added the "Join", with it you get the array and concatenates with what was specified in the method in this case the comma

  • As I understood in your example, if select has 4 selected items, for example, foreach will run this code 4 times to insert item by item in the database. What I want is to save the array in a single field, as it is part of a single record per row in the table.

  • I understand, but you have these items separated by comma?

  • Yes, because after another time I will show this on the page for the user, probably using a php explode.

  • I just got home, I’ll change the answer, just a minute

  • 1

    I made the change to leave the way you want.

  • 1

    Perfect!! Thanks for the help and attention!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.