I'm using joomla and have a template override on my articles to display a deal. The deals are stored in sql database so I retrieve the stored information using php. See below:
$query->clear();
$query->select('*');
$query->from($db->quoteName('#__deal_option'));
$query->where($db->quoteName('deal_id')." = ".$cmgbstore_id ." AND (". $db->quoteName('option_id')." = 1 OR 2 OR 3)");
$db->setQuery($query);
$db->execute();
$num_rows = $db->getNumRows();
The deals have more than one option so i retrieve the deals using the following:
$row = $db->loadRowList();
$opt1_id = $row['0']['2'];
$opt1_o_price = $row['0']['4'];
$opt1_d_price = $row['0']['5'];
$opt1_title = $row['0']['3'];
$opt1_save = $opt1_o_price-$opt1_d_price;
The same code is used to retrieve the other options by changing the row and gets stored to the same variables but the number change eg. option two's id would be stored in $opt2_id
.
My issue is I need users to be able to select the option they want and dynamically update on the website without using a submit button. Basically when the option is selected the price updates automatically.
To fix this I used a radio box selection. See code below:
<div class="product-options-contain">
<?php
if ($num_rows >= '1') {
?>
<div class="product-options">
<div class="option-input">
<input type="radio" value= "1" name="product-option" class="radio" checked="checked">
</div>
<label class="option-detail">
<span class="option-title"><?php echo $opt1_title ?></span>
<span class="option-price">R<?php echo $opt1_o_price ?></span>
<span class="option-was-price">R<?php echo $opt1_d_price ?></span>
<span class="option-discount">Save R<?php echo $opt1_save .'.00'?></span>
<span class="option-bought">Over 31 bought</span>
</label>
</div>
<?php
}
?>
<?php
if ($num_rows >= '2') {
?>
<div class="product-options">
<div class="option-input">
<input type="radio" value= "2" name="product-option" class="radio">
</div>
<label class="option-detail">
<span class="option-title"><?php echo $opt2_title ?></span>
<span class="option-price">R<?php echo $opt2_o_price ?></span>
<span class="option-was-price">R<?php echo $opt2_d_price ?></span>
<span class="option-discount">Save R<?php echo $opt2_save .'.00'?></span>
<span class="option-bought">Over 31 bought</span>
</label>
</div>
<?php
}
?>
<?php
if ($num_rows >= '3') { ?>
<div class="product-options">
<div class="option-input">
<input type="radio" value= "3" name="product-option" class="radio">
</div>
<label class="option-detail">
<span class="option-title"><?php echo $opt3_title ?></span>
<span class="option-price">R<?php echo $opt3_o_price ?></span>
<span class="option-was-price">R<?php echo $opt3_d_price ?></span>
<span class="option-discount">Save R<?php echo $opt3_save .'.00'?></span>
<span class="option-bought">Over 31 bought</span>
</label>
</div>
<?php
}
?>
</div>
As you can see the option is only visible if the option exists. Now where I'm stuck is retrieving the value that has been selected in the radio button.
I have a script that does allows me to view the value of the selected option via an alert:
<script type="text/javascript">
function getval(sel)
{
alert(sel.value);
}
</script>
But I dont need the alert, I need a value. And I need to be able to use the value to dynamically change the information displayed. I'm guessing I require AJAX to complete this but I have tried many different combinations but nothing seemed to work.
I have found a script that works on the same principle but I have no idea on how to implement it into my code.
$(function () {
$('.product-options input:first').attr('checked', 'checked');
})
$(document).on('change', '.product-options input', function () {
var productId = $(this).attr('data-productId');
var price = $(this).attr('data-price')
var discount = $(this).attr('data-discount')
var save = $(this).attr('data-save')
$('[data-id]').attr('data-id', productId);
$('.savings .value.selling-price .value-amount').html(price);
$('.savings .value.discount .value-amount').html(discount);
$('.savings .value.save .value-amount').html(save);
})
To be honest I don't even know how it works.
I really need help and any assistance would be greatly appreciated.