CodeIgniter Forums
Change currency from dropdown menu - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: General Help (
+--- Thread: Change currency from dropdown menu (/thread-63429.html)

Change currency from dropdown menu - Lykos22 - 10-30-2015

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' nullTRUE); ?>>$ 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>

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');

// 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(){
selected = $(this).val();
// alert(selected);
$.post('<?php echo site_url( $this->uri->segment(1) . '/setcurrency'); ?>', {currencyselected}, function() {

How can I set the new currency in session and then refresh the page with the new currency set ?

RE: Change currency from dropdown menu - PaulD - 10-30-2015

I am not sure what your problem is with this code as it looks like it should work. Are you getting an error or is the logic not working or is the refresh not reloading with new data?

I am not sure if this will help at all, but the location.reload() has an extra parameter, that when set to TRUE reloads the page from the server, otherwise it reloads it from the browser.

forceGet : Boolean
Optional. Specifies the type of reloading:
• false - Default. Reloads the current page from the cache.
• true - Reloads the current page from the server

If this has completely misunderstood the problem you are having, I apologise in advance,
Best wishes,