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".
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p></p>
<b>NºMarcaçã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ódigo 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
We know what you want, and how you tried, but what problem did you have? Any errors? Questions regarding the quality of the code?
– Costamilam
text already came by default does not change I wanted it to change if when I point another select option
– Bruno Teixeira
What text? What changes? What should happen when changed? What is happening? Try to be as clear and specific as possible
– Costamilam
See where it says Price:... Discount:... Total Sale:.... I wanted to change when I change the option changes the values of each text
– Bruno Teixeira
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
– Bruno Teixeira
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
– Costamilam
Doesn’t answer my question
– Bruno Teixeira
Imagine that I selected another service and another voucher the texts do not change numbers.
– Bruno Teixeira
Let’s go continue this discussion in chat.
– Bruno Teixeira