I’ve set up a currency conversion dropdown in a wordpress site.
The only thing missing is that every time I load another page, the currency will reset as the form selection was ‘forgotten’.
Any ideas how to do this? I tried a suggested js cookie that I saw here, but it doesn’t work.
This is what I got so far:
<form name="myform" id ="myform" method="post">
<select name="currency-select" id="sort" onchange="submitform();">
<option value="" selected="selected">Currency</option>
<option value="0">U.S Dollars (USD)</option>
<option value="1">Euros (EUR)</option>
<option value="2">British Pounds (GBP)</option> `
</select>
</form>
js:
function submitform()
{
document.myform.submit();
}
I tried using this code as recommended here but it doesn’t really work out for me, I think I didn’t do it the right way –
<?php
`session_start();`
if (isset($_POST['currency-select'])) {
$_SESSION['sort'] = $_POST['sort'];
}
?>
I added the $_SESSION to the form as well:
<option value="0" <?php if($_SESSION['sort'] == "0") echo "selected";?>>U.S Dollars (USD)</option>
UPDATE
I’ve made a few tests. The session seems to be saved (as I echoed it on a few pages while refreshing etc.) I guess the only problem now is related to the form itself. Even with the right session number, I can’t get it to select the right option.
I’ve tried two methods, but both does not work:
<option value="0" <?php if($_SESSION['currency-select'] == "0") echo 'selected="selected"';?>>U.S Dollars (USD)</option>
or
<option value="0" <?php if($_SESSION['currency-select'] == "0") echo "selected";?>>U.S Dollars (USD)</option>
I’d store the selected value in a
$_SESSION['selected_currency']
variable and the cross check and select it when the drop down is being populated with the currency list.Assuming that the sessions are working, I will use something like below to keep the currency selected in your drop down.
There could be shorter ways, I am using this for illustration purposes.
For permanent retain of data you only have a few possibilities, the easiest to implement are $_SESSION, $_COOKIE or in a Database.
You have two options to do that
1st is by adding a field to the
options.php
page and save your data then get back your data from theoptions.php
for that you’ve to useupdate_option('nameOfField_form','nameOfFieldDb');
andget_option('nameOfFieldDb')
.and 2nd option is by
jquery.ajax();
method save your data inoptions.php
you may find these links helpful codex
get_option
update_option