Hello. I am using a lightbox form with a drop-down list. When I choose a value from the dropdown list, I want it to be assigned to a php variable which I will then use to update the total price of the form (the total price is already defined in the form). Here is the link to the relevant page: https://mobilitypro.eu/course/e-learning-blended-learning-and-introduction-to-web-designing/ . When the user clicks Book Now, the lightbox form appears. The problem is with the dropdown list "Select package": when an option is selected, I cannot pass its numerical value to a php variable (before submitting the form). I tried with Ajax to send the value to another php file, but it did not work. Here below is my code, as well as some related pre-existing code of the lightbox-form. I look forward to your help, as I have been trying to find a solution for a long time.

<script>
function getval(sel)  
{
   var singleValues = sel.value;

   var price = <?php echo $price ?> * singleValues; 
   alert(price); //this is working

} 
</script> 
<p class="gdlr-lms-half-right"> 
<span><?php _e('Select package*', 'gdlr-lms'); ?></span> 
<select id="package" onchange="getval(this);"> 
<option value="1">Package 1</option> 
<option value="1.5">Package 2</option> 
<option value="2.55">Package 3</option> 
</select> 
</p> <?php $price=$price ?> //this is not working

Pre-existing code preceding my code:

<?php

            $price = empty($course_option['discount-price'])? $course_option['price']: $course_option['discount-price'];
            $price = floatval($price);
        ?>

Pre-existing code following my code:

<p class="gdlr-lms-half-right"> <?php _e('Total Price', 'gdlr-lms'); ?> 
<input type="text" class="price-display" value="<?php echo gdlr_lms_money_format(($price * $fix_val['amount']) - $fix_val['coupon-discount']); ?>" disabled /> 
<input type="hidden" class="price" name="price" value="<?php echo (($price * $fix_val['amount']) - $fix_val['coupon-discount']); ?>" /> 
<input type="hidden" class="price-one" name="amount" value="<?php echo $price; ?>" /> 
<input type="hidden" class="format" value="<?php echo $lms_money_format; ?>" /> </p>

This looks like a mis-understanding of PHP, the code you want is a javascript solution:

<script type='text/javascript'>
var price = <?php echo $price;?>;//make it global outside the function

function getval(sel){
    var singleValues = sel.value;
    var thisPrice = price * singleValues;
    alert(thisPrice);
    document.getElementById('selectedItem').innerHTML = sel.val+' Selected. Price: '+thisPrice;
}
</script>

<select id='package' onchange"getval(this);">
</select>

<div id='selectedItem'></div>

PHP runs before the page is viewed so you can't use the PHP script to interact directly on the page, the code will run at page start. Javascript is a coding language that runs on the web browser so you can do things on the page anytime after it has loaded.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.