-1
Hello, I am trying to make an autocomplete in the unit value field according to the product name chosen in select, select options filled in according to the products of the database
The logic I developed was to execute a query in PHP that selects Description and Price and fills the tag with value = price and content of the option receiving Description. Then I used the javascript DOM to get the value of the option chosen in the "onblur" event and a PHP variable receives this content. With the PHP variable receiving value, I wanted to fill this value in the unit field
<label>Produto: </label><br>
<select name="nomeproduto" onblur="valorSelect()">
<option value=""></option>
<?php
$sql = "select descricao, preco_venda from produtos";
$result = $conn->query($sql);
while($rows = $result->fetch_assoc()) {
?>
<option value="<?php echo $rows['preco_venda']; ?>">
<?php echo $rows['descricao']; ?>
</option>
<?php } ?>
</select><br><br>
<?php
$valor =
"<script>
function valorSelect() {
let valor = document.querySelector('select').value;
document.write(valor);
}
</script>";
?>
<label>Valor unitário: </label><br>
<input type="number" name="valorunitario" value="<?php echo $valor; ?>">
Who can help to correct these mistakes I will be very grateful
Thanks, I was able to solve the problem, but I will test your logic <select name="product name" onblur="valueSelect()"> Function valorSelect() { Let value = Event.target; value = parseFloat(value.value) Let display = Document.getElementById('price') display.value = "<? php $phpvar='"+value+"'; echo $phpvar;? >"; } <input id="preco" type="number" name="valorunitario" value="">
– Samuel Lucas
Show! Try to come up with a logic to leave this javascript in an external file, for better performances, since it will enter the browser cache.
– andre_luiss