Retrieve select value via JS and play in PHP variable

0

I need to get the selected value of the select and play in a query , how do I? In practice (following the example) I choose to select a driver and this select will return below all the records regarding this driver, maybe the fields could be date of trip, vehicle, etc ... (it's just an example), all this without a submit button or any other.

I have the following select list:

<select name="regiao" onchange="run()" id="regiao">
 <option value="0" selected="selected">Escolha a Região</option>
 <?php 
 //listar regioes
    $db->select_pdo("SELECT * FROM regiaos order by indOrdem ASC"); 
    foreach($db->result as $value){
      echo '<option value="'.$value['idRegiao'].'">'.$value['txtDescricao'].'</option>';
    } 
 ?>
</select>

E o Javascript:

<script>
   function run() {
    document.getElementById("rstregiao").value = document.getElementById("regiao").value;
   }
</script>

E o resultado (que imprime o html c o valor correto, porém eu precisaria do valor sem o html para rodar a variável na query): 

<?php 
  $regiaos = '<input type="text" id="rstregiao" placeholder="valor">';
    echo $regiaos; 
    //listar redecredenciadas 
    $db->select_pdo("SELECT * FROM redecredenciadas WHERE idRegiao = '$regiaos' order by txtNome ASC"); 
    foreach($db->result as $value){
      unset($credenciada);  
      $credenciada = $value['txtNome'];
        echo $credenciada; }
?>

----- >

Now I have the following situation:

COMBO refreshes the query page, the result returns and is printed, but in printing I can not make the formatting of the loop consistent with the table, below my files for anyone who can help, and of course @ Thomas also has helped and much! (Thank you very much)

- > JAVASCRIPT

<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script><script>$(document).ready(function(e){$("body").delegate("#regiao", "change", function(data){

                //Pegando o valor do select
                var valor = $(this).val();
                //Enviando o valor do meu select para ser processado e
                //retornar as informações que eu preciso
                $("#conteudo").load("regiaos.php?parametro="+ valor);

            });
        });
</script>

- > QUERY PAGE

<?php
include ("conn.php");

$parametro = $_GET['parametro'];
$db->select_pdo("SELECT * FROM redecredenciadas WHERE idRegiao = '$parametro' ORDER BY idRegiao ASC");
    foreach($db->result as $value){
    echo '<tr>
            <td>- '.$value['txtNome'].'</td>';
    }
?>

- > PAGE WITH THE PRINTED RESULT (with the wrong formatting for the TAG that needs to have

            Accredited Unit Name             Active             AOP             Pop             APC             HO             PSI             PSO             H             Ps             M             PAN             Hp             AMB             PS Inf                                                                                                                                                                                                                              

- > THE RESULT IN THE SCREEN

Accredited Unit Name | Active | AOP | POP | APC | HO | PSI | PSO | H | PS | M | PA | HP | AMB | PS Inf - unit 01 | chk | ch | ch | ch | ch | ch | ch | ch | ch | ch | ch | ch | ch | chk - unit 02 - unit 03 etc ... and the chechbox (chk) are not inside the loop the units are in a single cell (I said it was ridiculous)

Thank you

    
asked by anonymous 19.01.2015 / 13:24

1 answer

1

Partner, I see 2 solutions to this:

1st - When calling the run () function in JavaScript, get the value of your select and use it in a redirect passing it as a parameter:

var valor = document.getElementById("regiao").value;    
window.location = "pagina.php?parametro=valor"

2nd - Particularly, I consider this second solution the best: Use jQuery. You can use the $ .get (), $ .post (), $ .ajax () methods, or even use the .load () method. This way you will be able to do your query without using refresh ().

Here is an example code:

index.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>

        <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script><script>$(document).ready(function(e){$("body").delegate("#regiao", "change", function(data){

                //Pegando o valor do select
                var valor = $(this).val();
                //Enviando o valor do meu select para ser processado e
                //retornar as informações que eu preciso
                $("#conteudo").load("query.php?parametro="+ valor);

            });
        });
        </script>
    </head>

<body>
<select name="regiao" id="regiao">
    <option value="1">Marcos da Silva</option>
    <option value="2">João da Silva</option>
    <option value="3">Roberto da Silva</option>
</select>

<div id="conteudo"></div>
</body>
</html>

query.php

<?php

$parametro = $_GET['parametro'];

//Aqui você pode fazer a sua query, e buscar os dados de um banco de dados

?>
<table>
    <thead>
        <tr>
            <th>Nome</th>
            <th>Idade</th>
            <th>Próxima Viagem</th>
            <th>Veículo</th>
        </tr>
    </thead>
    <tbody>
        <?php if ($parametro == 1): ?>
            <tr>
                <td>Marcos da Silva</td>
                <td>30</td>
                <td>01/02/2015</td>
                <td>Fiorino</td>
            </tr>
        <?php endif; ?>
        <?php if ($parametro == 2): ?>
            <tr>
                <td>João da Silva</td>
                <td>45</td>
                <td>21/02/2015</td>
                <td>Sprinter</td>
            </tr>
        <?php endif; ?>
        <?php if ($parametro == 3): ?>
            <tr>
                <td>Roberto da Silva</td>
                <td>25</td>
                <td>10/03/2015</td>
                <td>Doblo</td>
            </tr>
        <?php endif; ?>
    </tbody>
</table>

In the query.php file you make your query and bring the data and format it any way you want. See that every time you change the value of select, it calls query.php and performs what I want.

I hope I have helped =)

    
20.01.2015 / 18:35