0
I’m trying to pass the values of a select Multiple pro PHP. I have already researched examples on the internet (including my code is the same as the examples I saw), but the "echo" command does not show me anything. Can you help me ? Thank you.
$funcao = $_POST['funcao'];
$frutas = $_POST['frutas'];
if($funcao == 'copiar') {
foreach ($frutas as &$item) {
echo "Frutas escolhidas: ".$item."<br>";
}
exit;
}
<form method="post" name="frmfrutas" action="rascunho.php" id="frmfrutas">
<input type="hidden" name="funcao" id="funcao" value=""/>
<select class="slMultiple" multiple="multiple" name="frutas[]" id="frutas" size="10">
<option value="1">Maçã</option>
<option value="2">Banana</option>
<option value="3">Limão</option>
<option value="4">Morango</option>
<option value="5">Uva</option>
<option value="6">Amora</option>
</select>
<input type="button" value="SELECIONAR" class="btn btn-primary" id='btSelecionar' name='btSelecionar'/>
</form>
$(document).on('click', '#btSelecionar', function(event) {
event.preventDefault();
$("#funcao").val("copiar");
var self = $(this);
$.ajax({
url: "/rascunho.php",
type: "POST",
timeout:default_timeout,
data: $("#frmfrutas").serialize(),
beforeSend: function(){
self.attr('disabled', 'true');
},
success: function() {
},
error: function(jqXHR, textStatus){
console.log(textStatus, jqXHR);
},
complete: function(){
self.removeAttr('disabled');
}
});
});
I did a test with your code and it worked for me. you have to make the call by ajax? Maybe your problem is elsewhere.
– Tony
Seriously ? = ( On my screen does not appear the listing... On the console I can see the selected fruits but on the screen does not appear ;/
– goldenleticia
I guess in that case you have to use the
serializeArray
instead ofserialize
on the date of the ajax. From aprint_r($_POST)
in PHP to see what’s coming.;;– JuniorNunes
That’s exactly what I’m talking about. By the code you posted it works exactly for what was proposed, the data is passed by ajax and "echo" shows on the console what was printed. If you want to update some other part with the return of ajax, you have to adjust the "Success" of your ajax call. Imagery.
– Tony
Thank you all for your help. I removed the
serialize()
and passed the parameters one by one. It worked ;)– goldenleticia