fteng Posted October 10, 2009 Share Posted October 10, 2009 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. Link to comment https://forums.phpfreaks.com/topic/177220-toggling-currency-passing-variables/ Share on other sites More sharing options...
fteng Posted October 11, 2009 Author Share Posted October 11, 2009 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> Link to comment https://forums.phpfreaks.com/topic/177220-toggling-currency-passing-variables/#findComment-934649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.