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.
– Wictor Chaves
@Wictorchaves I want to save in array format even (1,2,3,5...).
– Henqsan
But in php you’re not treating it as an array, I’ll give you an answer so you understand what I’m saying.
– Wictor Chaves