How to display a result using 2 selects

Asked

Viewed 648 times

1

I have two table, Products and Orders.

I need to create a combobox for products and when the user choose a product, the page has to display all orders related to the product, if I choose another product have to change orders.

I’m thinking of using a Select from the product table to generate the products and then pick up the selected product id and make a select with the orders table.

Ex.:

Produto: Detergente          
 button: Gerar relatório

Pedido|Quantidade

0123  | 2 und

0258  | 100 und

Code below:

Produto:<SELECT NAME="produto"  required="1" onchange="geratabela()">
            <option>Selecione...</option>
            <?php
            $conexao = mysql_connect("localhost","root","usbw");
            if (!$conexao) die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error()); 
            mysql_select_db('Varejo', $conexao);
            $query1 = "SELECT * FROM produto ORDER BY cod_prod, nome ASC";
            $q1 = mysql_query($query1);
            while($dados = mysql_fetch_array($q1))
            {   
            ?>
             <option value="<?=$dados['cod_prod'] ?>">
            <?=$dados['nome'] ?>
            </option>
            <?php
                }
                ?>

                </SELECT><br><br>

        <?      
                function geratabela() {

                 ini_set( 'display_errors', true );
                error_reporting( E_ALL );
                $conexao = mysql_connect("localhost","root","usbw");
                if (!$conexao) die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error()); 
                mysql_select_db('Varejo', $conexao);
                $cod_prod = $_GET["cod_prod"];
                $sql = "SELECT * FROM pedido where cod_prod=$cod_prod";
                $q = mysql_query($sql);
                //tabela com os dados


                }
                ?>
  • What do you already have? Your problem is in select or combo box ? Or both?

  • Use Ajax, use select onchange or button onclick to query by ajax...

  • @War actually I have only the screen mounted but I’m thinking of logic so I asked why I’m not very good at programming.

  • @Mayronceccon Thanks I will search on the internet, you have some reference site ?

  • @Gustavob if you are using Jquery http://api.jquery.com/jquery.ajax/ or only JS http://www.w3schools.com/ajax/ and to create table http://www.arquivodecodigos.net/dicas/javascript-howto create a tag- list

  • @Mayronceccon Thanks for your help!

  • @Mayronceccon I generated an Ajax code in the notepad(I left the note at home), I will leave it along with the pegunta.

Show 2 more comments

1 answer

2


In case you are trying to call a PHP method which is "server side" with javascript which is "client side" in this case.

The ideal is to make another file to handle your AJAX request for example:

//fiz alterações para usar o PDO do php 

see reason here

//aqui vamos verificar se seu parametro foi passado
if(!isset($_POST["cod_prod"]){
    echo '';
    exit;
}
ini_set( 'display_errors', true );
error_reporting( E_ALL );
$dsn = "mysql:host=localhost;dbname=Varejo;charset=utf8";
$conexao = new PDO($dsn, 'root', 'usbw');    
$cod_prod[] = $_POST["cod_prod"]; // o ideal aqui é sanitizar os dados para não ter problema de segurança
$stmt = $pdo->prepare('SELECT * FROM pedido where cod_prod= ?');
$stmt->execute($cod_prod);
$ret = "<ul>"; // aqui vamos começar montar o html de retorno
foreach($stmt->fetchAll() as $value){
   $ret .= "<li>$value</li>";
}
$ret .= "</ul>";
echo $ret;

your html would look like this let’s call the file top of requests.php

ajax example:

<javascript>
function getPedidos(valor){
$.ajax({
method: "POST",
  url: "pedidos.php",
  data: { cod_prod: valor }
})
  .done(function( data ) {
    //faz algo com o valor retornado
  });
}

I recommend using Jquery

  • Thank you, I will test and give you the feedback. In the case of ajax I would implement it in html? And the link to Jquery is pointed to the wrong link.

  • This Ajax implements in html, I arranged the link. Remember to load the lib. Another thing, I did not test the code so maybe I need some adjustments. Good luck

  • opa I was able to fix the html part, I was able to solve the problem

Browser other questions tagged

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