Rolinda Strijker Rolinda Strijker - 7 months ago 14
HTML Question

Show option value in another div

In my database I have a table where I can add width, height and the matching price for this width x height.

Now I have the piece of code that shows the width x height in my dropdown menu on the site.
But the only thing I can't get to work that if I click on a width x height in my dropdown that the matching price of it shows somewhere else on my page.

<?php
$query = "SELECT * FROM shop_customoptions";
$stmt = $conn->query( $query );

$dropdown = "<select name='id' id='formaat_selecteren'>";
foreach ($stmt as $row) {
$dropdown .= "\r\n<option value='{$row['prijs']}'>{$row['breedte']} x {$row['hoogte']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>


Can someone help me accomplish this?

I already found a solution with javascript:

<script type="text/javascript">
$("#formaat_selecteren").change(function() {
if ($.isNumeric($(this).val())) {
$(".price").html("&euro; " + $(this).val());
} else {
$(".price").empty();
};
});
</script>


But it doesn't show the price with a , and I rather have a solution with PHP if there is one.

Answer

Give this a try

<script type="text/javascript">
    $("#formaat_selecteren").change(function() {
    if ($.isNumeric($(this).val())) {
    $(".price").html("&euro; " + String($(this).val()).replace('.',','));
    } else {
    $(".price").empty();
    };
    });
</script>
Comments