Welcome Guest, Not a member yet? Register   Sign In
Change currency from dropdown menu
#1

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>
</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'); ?>', {currencyselected}, function() {
            
location.reload();
        });
    });
</
script

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

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.

Quote:location.reload(forceGet)
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,

Paul.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB