Remembering php form data to display currency all over a website

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.

Read More

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>

Related posts

Leave a Reply

3 comments

  1. 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.

    <select name="currency">
    <?php
    foreach($currency as $value){
       if($value->currency_code == $_SESSION['currency']){
         echo "<option value='$value->currency_code' selected='selected'>$value->currency_name</option>";
       } else {
          echo "<option value='$value->currency_code'>$value->currency_name</option>";   
       }
    }
    ?>
    </select>
    

    There could be shorter ways, I am using this for illustration purposes.

  2. 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 the options.php for that you’ve to use update_option('nameOfField_form','nameOfFieldDb'); and get_option('nameOfFieldDb').

    and 2nd option is by jquery.ajax(); method save your data in options.php

    you may find these links helpful codex

    get_option

    update_option