Select SQL SERVER PDO data for a select list with PHP

Asked

Viewed 63 times

0

Dear ones, I have designed this code snippet so that the data received from my SQL SERVER database is listed in a select via PDO:

consultationphp.:

<form name="cargo" method="post" action="consultacargos.php">
 <label for="">Selecione um Cargo</label>
 <select>
 <option>Selecione...</option>
 
 <?php 
  $consulta = $conn->query("SELECT * FROM [DB].[Cargos] order by Codigo");
 while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) { ?>
  <option name ='selecao' value="<?php echo $linha['codigo'] ?>"><?php echo $linha['descricao'] ?></option>
 <?php } ?>
 </select>
    <button type="submit">Selecionar</button>

So far so good, the options appeared on the list of select right, only what I wish is that when selecting one of the options it is printed in the same page with an 'echo', you can help me?

I believe it is something simple, but I am in the process of learning and I was indicated to seek help in the stack for learning. Thanks in advance.

  • By the time the page is delivered to the browser, the php service is over. I suggest studying server-side vs client-side code in web development.

1 answer

0


This you can do using Javascript. Here is an example:

<select id="combo" onchange="selecionar()">  
     <option value="0">Selecione...</option>
     <option value="1">Valor 1</option>
     <option value="2">Valor 2</option>
     <option value="3">Valor 3</option>
</select><br><br>
<div id="resultado">Aqui vai aparecer o valor selecionado<br>

<script type="text/javascript">
function selecionar() {
        document.getElementById("resultado").innerHTML = document.getElementById("combo").value;
    }
</script>

Browser other questions tagged

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