how to use the select case from html

Asked

Viewed 950 times

0

Good morning, everyone,

I need to pull data from the database, but wanted the user to be able to choose from which data value to search.

My code html:

<head>
    <title></title>
</head>
<body>
<form method="POST" name="frmBusca" action="Exibindo.php">

Pesquisar pedido:<input type="text" name="busca" id="busca">
<input type="submit" value="Buscar" name="buscar">
 <select name="opcao">
        <option>Numero bl</option>
        <option>Conteiner</option>
    </select>
</form>
</body>

Now there in the php:

<?php 
$conexao = mysql_connect("localhost", "root", "") or die(mysql_error());
$banco = mysql_select_db("cliente_svn") or die(mysql_error());
$busca = $_POST['busca'];
$opcao = $_POST['opcao'];

case ($opcao = "Numero bl"):

I wanted to know the correct format to call the case.

Thank you!

  • could post the code HTML and the part where you request it with the PHP in the database ?

  • I’m trying to post the HTML but it’s compiling the code!

  • <!DOCTYPE html> <html> <head> <title></title> </head> <body> <form method="POST" name="frmBusca" action="Displaying.php"> Search request:<type input="text" name="search" id="search"> <input type="Submit" value="Search name"> <select name="option"> <option>Number Bl</option> <option>Container</option> </select> </form> </body> </html>

  • a piece of php

  • $query_select = "SELECT * FROM new_request WHERE num_bl = '$search'"; $select = mysql_query($query_select,$connected); if ($select) { echo "<form class='col_md_16'>"; echo "<label>Container listing</label> <table class='table-Erika'><tr><td>Container</td> <td>Size</td> <td>Weight</td> </tr>"; while ($escrever = mysql_fetch_array($select)) { echo "<tr><td>". $escrever['conteiner']." </td><td>". $escrever['tamanho_cont']." </td><td>". $write['peso_cont']." </td></tr>";

  • What’s wrong with your code? What doesn’t work ?

  • I didn’t quite understand your question, but you should make some adjustments to your code, for example, the input search has the name seek, but in the php you try to catch with the POST search. And their options in the select are without value.

  • I edited my question there, see if now you can understand.

Show 3 more comments

2 answers

2

If I understand the question, you can do so :

HTML

<form method="POST" name="frmBusca" action="Exibindo.php">
    <label for="busca">Pesquisar pedido:</label>
    <input type="text" name="busca" id="busca">
    <select name="opcao">
        <option value="1">Numero bl</option>
        <option value="2">Conteiner</option>
    </select>
    <input type="submit" value="Buscar" name="buscar">
</form>

PHP

$conexao = mysql_connect("localhost", "root", "") or die(mysql_error());
$banco = mysql_select_db("cliente_svn") or die(mysql_error());
$busca = isset($_POST['busca']) ? $_POST['busca'] : '';
$opcao = isset($_POST['opcao']) ? $_POST['opcao'] : '';

switch($opcao){
    case '2':
        $column = "conteiner";
        break;
    default: // se for qualquer outro valor imprevisto, inclusive o 1
        $column = "num_bl";
}   

$query_select = "SELECT * FROM novo_pedido WHERE $column = '$busca'"; 

It would be nice to also use mysqli.

Why should we not use 'mysql' functions_*' ?

0

I think I understand what you want, to be more dynamic would use jQuery,.

HTML

<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>

<label>Pesquisar pedido:<input type="text" name="busca" id="busca"></label>
<input type="button" value="Buscar" id="buscar">
<select name="opcao">
   <option value="Numero_bl">Numero bl</option>
   <option value="Conteiner">Conteiner</option>
</select>
<div id="resultado"></div>
<script>
$(document).ready(function(){
   $("#buscar").click(function(){
     $("#resultado").load("arquivo.php");
   });
});
</script>
</body>

Put in the value of <options> the name of the columns exactly you want to search as you select.

php file.

 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "cliente_svn";
 $conn = new mysqli($servername, $username, $password, $dbname);
 if ($conn->connect_error) {
    die("A Conexão Falhou: " . $conn->connect_error);
 }


$busca = $_POST['busca']; //blablabla
$opcao = $_POST['opcao']; //Numero_bl
//digamos que o numero bl seja a coluna;

$select = "SELECT * from tabela where $opcao = '$busca'";
$result = $conn->query($select);
while($row = $result->fetch_assoc()){
   echo "<p>".$row['nome_da_coluna']."</p>";
}

Browser other questions tagged

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