Store in a variable a data of the option marked in a select to then show it dynamically

1

I have a select where I complete the options with the data of the products that I have stored in my bbdd. The code is as follows:

    <select name="p1" id="select1" required />
    <option selected="true" disabled="disabled">Elige un producto</option>
    <?php   
    foreach($productos as $prod)
    {
    echo "<option value=".$prod['id'].">".$prod['nombre']." (".$prod['precio']." €)</option>";
    }
    ?>
    </select>

What I need is to store the value of $ prod ['price'] of the selected option, to then perform an arithmetic operation and display it elsewhere in the page dynamically.

On some previous occasion I used the onChange event to launch a function js and show the selected value but I do not know if it is possible mixing it with php. Also in this case the "value" corresponds to another data that I must save in the bbdd What would be the appropriate option?

    
asked by Tonio 25.03.2018 в 15:23
source

3 answers

2
<select name="p1" id="select1" required />
    <option selected="true" disabled="disabled">Elige un producto</option>
    <?php foreach($productos as $prod): ?>
    <option precio='<?php echo $prod['id'] ?>' value='<?php echo $prod['id'] ?>'><?php echo $prod['nombre']; ?></option>;
    <?php endforeach; ?>
</select>

I would do it that way it's cleaner, with jquery ($ ("# select1"). attr ('price');) you can calculate the price and save it in another variable within the field.

Also what you can do is process it with PHP. Ask your BBDD for the ID and calculate the price.

    
answered by 25.03.2018 / 15:58
source
1

Using the JSON extension and following the idea of how you have the code, the following occurs to me:

<select name="p1" id="select1" required />
	<option selected="true" disabled="disabled">Elige un producto</option>
	<?php   
		foreach($productos as $prod)
		{
			echo "<option value='{".htmlspecialchars('"'.$prod['id'].'"', ENT_QUOTES, 'UTF-8').":".htmlspecialchars('"'.$prod['precio'].'"', ENT_QUOTES, 'UTF-8')."}'> ".$prod['nombre']." ( ".$prod['precio']." €) </option>";
		}
	?>
</select>

The previous code leaves each "value" in the following way:

"{"id_producto":"precio_producto"}"

Then using the extension JSON and its function json_decode (the product id as well as the price must be enclosed in double quotes in order to use json_decode):

json_decode($_POST['p1'], true)

This what it does is to return an associative array of the form:

[id_producto] => precio_producto

With this you obtain the ID and price of the product of the selection in a relatively simple way. I hope you serve, greetings.

    
answered by 26.03.2018 в 01:20
1

Thanks for the answers. In the end I did it with jquery, something similar to what @Subiendo suggests.

<select name="p1" id="select1" required />
<option precio="0" selected="true" disabled="disabled">Elige un producto</option>
<?php foreach($productos as $prod): ?>
<option class="opcion" precio='<?php echo $prod['precio'] ?>' value='<?php echo $prod['id'] ?>'><?php echo $prod['nombre']; ?></option>;
<?php endforeach; ?>

But I added a class to the options of the select so that when I clicked one it tells me the price.

The js would look like this:

$('.opcion').on('click', function(){
var $precioSeleccionado = $(this).attr('precio');
});
    
answered by 26.03.2018 в 13:37