How to send the chosen value within an option that has a query in the database

Asked

Viewed 38 times

-1

<?php session_start();
include 'conexao.php';
?>
<form action="recebe.php" method="POST">
  <select class="browser-default custom-select">
  <option name="arrays"value="nenhum">Nenhum</option>
    <option name="arrays"value="<?php ['id_cane']?>"><?php                                
      sql = mysqli_query($conn, "SELECT * FROM CNAE ") 
        or die(mysqli_error($conn));
   while($aux = mysqli_fetch_assoc($sql)) { 
     ?><option ><?php
    echo "".$aux["descricao"]."<br/>"; //outro atributo de toda pesquisa feito pelo $sql
 } ?>
      </option> </select>
    <button type="submit" class="btn btn-primary">Enviar</button>

</form>

recebe  o valor na pagina recebe.php
<?php
    session_start();
    include 'conexao.php';  
    $arrays = $_POST["arrays"];   

    echo "<script>
    alert ($arrays)           
  </script>";

?> 
  • 2

    Put an explanation of your problems and how your code works. If you don’t have a whim with your question it won’t be the others who will have you with your answer.

1 answer

0


From what I’ve seen of your code, you want to put together a list of options based on the contents of a table called CNAE. If this table has the fields "id_cnae" and "Description", the code to assemble the form and receive the value on the received page would be as follows.php:

<?
php session_start();
include 'conexao.php';
?>

<form action="recebe.php" method="POST">
 
 <select class="browser-default custom-select" name="arrays">
  
	<option value="nenhum">Nenhum</option>
    
	<?php                                
    sql = mysqli_query($conn, "SELECT * FROM CNAE") or die(mysqli_error($conn));
    while($aux = mysqli_fetch_assoc($sql)) { 
    ?>
	<option value="<?php echo $aux->id_cnae;?>"><?php echo $aux->descricao;?></option> 
	<?php
	}
	?>
	</select>
    <button type="submit" class="btn btn-primary">Enviar</button>
</form>


<?php
	//recebe  o valor na pagina recebe.php
    session_start();
    include 'conexao.php';  
	if (isset($_POST["arrays"])){
    $arrays = $_POST["arrays"];   
		print_r( $arrays);
	}
?> 

  • helped me, only it’s not showing the result of the search in the option, it’s the quantity but it doesn’t show; it was like this: <div class="form-group" > <label for="inputCNAE">Primary Activity</label> <br/> <select class="browser-default custom-select"> <? php $sql = mysqli_query($Conn, "SELECT id_cnae, Description FROM CNAE") or die(mysqli_error($Conn));&#Xa while($aux = mysqli_fetch_assoc($sql)) { ? > <option value="<? php print $aux->id_cnae;? >"><? php print $aux->Description;? ></option> <? php } ? > </select> </div>

Browser other questions tagged

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