Take select Multiple values

Asked

Viewed 9,845 times

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.

  • 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 ;/

  • I guess in that case you have to use the serializeArray instead of serialize on the date of the ajax. From a print_r($_POST) in PHP to see what’s coming.;;

  • 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.

  • Thank you all for your help. I removed the serialize() and passed the parameters one by one. It worked ;)

1 answer

3


1 - HTML - I made a change in 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 - draft.php, tested 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>";
 }
  • It worked out the model 2 here for me... I just replace the <br> comma and I used the $result in my query. Thank you !

Browser other questions tagged

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