I'd like some help please. I'm having a dropdown menu on the header of every page where I can change the currency set.
PHP Code:
<select id="set-currency" name="currency" class="form-control">
<option value="USD" <?php echo set_select('currency', ($this->session->userdata('currency_code') == 'USD') ? 'USD' : null, TRUE); ?>>$ USD</option>
<option value="EUR" <?php echo set_select('currency', ($this->session->userdata('currency_code') == 'EUR') ? 'EUR' : null); ?>>€ EUR</option>
<option value="GBP" <?php echo set_select('currency', ($this->session->userdata('currency_code') == 'GBP') ? 'GBP' : null); ?>>£ GBP</option>
</select>
I have also set a function in MY_Controller for setting the new currency in sessions, so this should apply in every page of my website
PHP Code:
public function setcurrency() {
// reset the currency
$currency = $this->input->post('currency');
$this->session->set_userdata('currency_code', strtoupper($currency));
// get the last visited uri string and clear the session, in order to keep track of the new selection
// $previous_url = $this->session->userdata('uri_string');
// $this->session->unset_userdata('uri_string');
// redirect to last visited page
// redirect($previous_url);
}
Finally this is my js script
PHP Code:
<script type="text/javascript">
$('#set-currency').on('change', function(){
var selected = $(this).val();
// alert(selected);
$.post('<?php echo site_url( $this->uri->segment(1) . '/setcurrency'); ?>', {currency: selected}, function() {
location.reload();
});
});
</script>
How can I set the new currency in session and then refresh the page with the new currency set ?