search results from a select

Asked

Viewed 46 times

0

I wanted from one or several selects to fetch information from the database and insert that data into the page as text. This is to be an "invoice".

Example: inserir a descrição da imagem aqui Code:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p></p>
<b>N&#186Marcação:</b> <input name="n_marcacao" type="number" size="1" id="n_marcacao">
<p></p>
<b>Serviço:</b>
<select name='servico' id='servico'>
<option></option>
<?php
    $sql = "SELECT cod_prod,nome_prod FROM produtos";
    $resultado = mysqli_query($ligacao, $sql) or die(mysqli_error());

    while($row = mysqli_fetch_assoc($resultado)) {
        echo '<option value="'.$row['cod_prod'].'">'.$row['nome_prod'].'</option>';
    }

?>
</select>
<br>
<p></p>
<b>Nome do Cliente:</b>
<select name='cod_cli'>
<option></option>
<?php
       $sql = "SELECT cod_cli,nome_cli FROM dados_cli";
       $resultado = mysqli_query($ligacao, $sql);

       while($row = mysqli_fetch_assoc($resultado)){
            echo '<option value="'.$row['cod_cli'].'">'.$row['nome_cli'].'</option>';
       }
    ?>
</select>
<br>
<p></p>
<b>Compareceu:</b>
<select name='compareceu' id='selecionar'>
<option>Selecionar a opção</option>
<option data-section="comp" value='1'>Compareceu</option>
<option value='0'>Não Compareceu</option>
</select><br>
<!--Se compareceu-->
<div data-name="comp" class ="hide">
<p></p>
<b>Usou Voucher:</b>
<select id="select" name="servico_com_voucher">
<option>Selecionar</option>
<option data-sections="usou" value="1">Usou</option>
<option value="0">Não Usou</option>
</select><br>
</div>
<!--Se usou Voucher-->
<div data-names="usou" class="hide2">
<p></p>
<b>C&oacutedigo do Voucher:</b>
<select name='cod_voucher' id='voucher'>
<option>Selecionar Voucher</option>
<?php 

       $sql = "SELECT cod_voucher FROM vouchers";
       $resultado = mysqli_query($ligacao, $sql);

       while($row = mysqli_fetch_assoc($resultado)){
            echo '<option value="'.$row['cod_voucher'].'"> '.$row['cod_voucher'].' </option>';
       }
    ?>
</select><br>
</div>
<p></p>
<input name="inserir" type="submit" id="inserir" class="button" value="Inserir venda"/>
</form>
<b>Preço:</b> <?php 
    $cod_prod=isset($_POST['servico']);
    $sql = "SELECT preco FROM produtos WHERE cod_prod LIKE '%".$cod_prod."%'";
    $resultado = mysqli_query($ligacao, $sql) or die(mysqli_error($ligacao));
    $row = mysqli_fetch_assoc($resultado);
    $valor_prod=number_format($row['preco'],2,","," ")."€";
    echo $valor_prod;
    ?>
<br>
<b>Desconto:</b> <?php
    $cod_voucher=isset($_POST['cod_voucher']);
    $sql = "SELECT * FROM vouchers INNER JOIN produtos ON vouchers.cod_prod=produtos.cod_prod WHERE cod_voucher LIKE '%".$cod_voucher."%'";
    $resultado = mysqli_query($ligacao, $sql) or die(mysqli_error($ligacao));
    $row = mysqli_fetch_assoc($resultado);
    if($row['tipo']==1){
        $valor_fix=number_format($row['valor'],2,","," ")."€";
        echo $valor_fix;
    }elseif($row['tipo']==2){
        $perc=$row['valor']*100;
        echo $perc."%";
    }elseif($row['tipo']==3){
        echo "Oferta: ".$row['nome_prod'];
    }
?><br>
<b>Total da Venda:</b> <?php
    if($row['tipo']==1){
        $valor_total=$valor_prod+$valor_fix;
        echo number_format($valor_total,2,","," ")."€";
    }elseif($row['tipo']==2){
        $valor_perc=$perc/100;
        $valor_total=$valor_prod*$valor_perc;
    }elseif($row['tipo']==3){
        echo "Oferta";
    }
?>

Tables used: https://imgur.com/a/zlUcoHg

  • 1

    We know what you want, and how you tried, but what problem did you have? Any errors? Questions regarding the quality of the code?

  • text already came by default does not change I wanted it to change if when I point another select option

  • What text? What changes? What should happen when changed? What is happening? Try to be as clear and specific as possible

  • See where it says Price:... Discount:... Total Sale:.... I wanted to change when I change the option changes the values of each text

  • For example, changing the service changed the price downwards and the total. If you change the code of the voucher I change the discount if you have not used voucher stay with the normal price

  • This answers your question? How to recover variables via Ajax query result in PHP? This is just one example, if you look for the terms PHP and AJAX, you will find several similar questions

  • Doesn’t answer my question

  • Imagine that I selected another service and another voucher the texts do not change numbers.

Show 4 more comments
No answers

Browser other questions tagged

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