Get select multiple values

0

I'm trying to pass the values of a select multiple pro PHP. I already searched for examples on the internet (my code is just like 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');
			}
		});
	});
    
asked by anonymous 11.04.2017 / 20:18

1 answer

1

1 - HTML - I made a change in the inputs:

<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>Maçã</option>
        <option>Banana</option>
        <option>Limão</option>
        <option>Morango</option>
        <option>Uva</option>
        <option>Amora</option>      

</select>
<button value="SELECIONAR" class="btn btn-primary" id='btSelecionar' name='btSelecionar'/>SELECIONAR</button>

</form>

2 - Script

$(document).on('click', '#btSelecionar', function(event) {
    event.preventDefault();
    $("#funcao").val("copiar");
    var self = $(this);
    $.ajax({type: "POST",
        url: "rascunho.php",
        type: "POST",
        timeout:default_timeout,
        data: $( "form" ).serialize();,
        beforeSend: function(){
            self.attr('disabled', 'true');
        },
        success: function() {

        },
        error: function(jqXHR, textStatus){
            console.log(textStatus, jqXHR);
        },
        complete: function(){
            self.removeAttr('disabled');
        }
    });
});

3 - rascunho.php, I tested it with the 3 codes.

  if (isset($_POST["funcao"])) {
    $optionArray = $_POST["frutas"];
    for ($i = 0; $i < count($optionArray); $i++) {
        echo $optionArray[$i]."<br>";
    }
  }

or

 if(!empty($_POST["frutas"]) and is_array($_POST["frutas"])) {
     $result = implode(' <br> ',$_POST["frutas"]);
     echo $result;
 }

Or

foreach ($_REQUEST['frutas'] as $selectedOption){
     echo $selectedOption."<br>";
 }
    
11.04.2017 / 23:22