Author Topic: Toggling currency passing variables  (Read 218 times)

0 Members and 1 Guest are viewing this topic.

Offline ftengTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Toggling currency passing variables
« on: October 10, 2009, 01:58:59 PM »
I'm trying to add currency dropdown to choose between two currencies. Using the onchange event, I reload the page and store the value in a session variable. However, the problem is that I use a lot of forms throughout my website and I pass variables around all over the place. When the page reloads, I lose all the $_REQUEST variables because of my currency toggle. I can pass the variables one by one, but I use a lot of variables and I'm worried I may miss one.

Is there a better way to design the currency toggling? Any help is much appreciated.

Here's the code version of what I said above:

<form method=post action=<?=$_SERVER['PHPSELF']?>>
<select name=currency onchange="form.submit();">
  <option value=1 <?=$USD?>>U.S. Dollar</option>
  <option value=2 <?=$CAD?>>Canadian Dollar</option>
</select>
</form>

Note: The $USD and $CAD variables are used to store which option is SELECTED.

Offline ftengTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Re: Toggling currency passing variables
« Reply #1 on: October 10, 2009, 09:59:15 PM »
Ok I got it working. I looked up how OSCommerce does this. Essentially, I think my logic is right, just pass all the hidden variables around. Hopefully, this helps someone else who has a similar problem.

Here's my final code:

   //Currency Change
   if (isset($_REQUEST['currency']))
      $_SESSION['currency'] = $_REQUEST['currency'];      

       //Default currency
   if (!isset($_SESSION['currency']))
      $_SESSION['currency'] = 1;   
        //Go to the right place in the dropdown
   if ($_SESSION['currency']==1)
      $USD = "SELECTED";
   else $CAD = "SELECTED";
   
        //Attach all the hidden request variables to the URL
   $URL = $_SERVER['PHP_SELF'];   
   $hidden_request_variables = '';
   foreach ($_REQUEST as $k => $v) {
                //Exclude currency since it may change
      if ( ($k != 'currency') && ($k != sessionID) && ($k != 'PHPSESSID') ) {
         $hidden_request_variables .= "$k=$v&";
      }
   }      
   $URL .=  "?{$hidden_request_variables}";
        //Remove the last character either & or ?
   $URL = substr($URL, 0, -1);

<form method=post action="<?=$URL?>">
<select name=currency onchange="form.submit();">
  <option value=1 <?=$USD?>>U.S. Dollar</option>
  <option value=2 <?=$CAD?>>Canadian Dollar</option>
</select>
</form>