0

I have drop down in pop up window where users can choose their country. I am using hidden field to submit choice with php and when I hit "submit" button I get the country which was chosen. Information about country is shown in the header so it should be on every page but when I start to browse between pages the country value dissapeares. How can I keep it on every page?

<div class="field">
            <div class="input-box">
                <?php $_countries = Mage::getResourceModel('directory/country_collection')->loadData()->toOptionArray(false); ?>
                <?php if (count($_countries) > 0): ?>
                    <select name="country" id="country" onchange="print(this.value)">
                        <option value=""> </option>
                        <?php foreach($_countries as $_country): ?>
                            <?php if(!in_array($_country['value'], $arrNO)):?>
                                <option value="<?php echo $_country['value'] ?>" >
                                    <?php echo $_country['label'] ?>
                                </option>
                            <?php endif;?>
                        <?php endforeach; ?>
                    </select>
                <?php endif; ?>
            </div>
            <form action="" method="post">
            <input id="choice" type="hidden" name="fname" value=""/>
            <input type="submit" value="OK"/>
            </form>
            <div id="usWarning"><p><span class="red">NOTE</span>: If you live in <span class="red">Arizona</span>, <span class="red">Iowa</span>, <span class="red">Maryland</span>, <span class="red">Oklahoma</span>, <span class="red">South Dakota</span>, <span class="red">Vermont</span>, <span class="red">Washington</span> or <span class="red">Wisconsin</span>, we are unfortunately not allowed to sell tobacco to you. Its forbidden with online sales of tobacco in these states.</p></div>
        </div>

"Value" for the hidden input field assigned with the help of JS:

function print(value) {
    document.getElementById("choice").value=value;
}

Thank you for your help.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
Curious Programmer
  • 127
  • 1
  • 2
  • 13

3 Answers3

0

You could make a session variable, to make the value persist across pages.

$_SESSION['countryname'] = "countryname";

Sessions

Edit:

As you are using Sessions for a different purpose, set a cookie, separately, subject to its own expiry. using setcookie.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
0

To preserve a form value across requests as a hidden field simply means echoing it back into the form when rendering it.

<input id="choice" type="hidden" name="fname" value="<?php echo htmlspecialchars ($_POST ['fname']); ?>" />

Alternatively you can use sessions or cookies to propagate the value to other pages.

GordonM
  • 31,179
  • 15
  • 87
  • 129
  • Tried that didn't work because when I browse other pages on this website `$_POST["fname"]` becomes empty. Problem with session that it expiers within 20 min, problem with cookie it doesn't save imideately when I hit submit button... :S – Curious Programmer Nov 09 '12 at 09:37
0

in action page

$_SESSION['country']    =   $_POST['country']

in form

<option value="<?php echo $_country['value'] ?>" <?php if(isset($_SESSION['country']) && $_SESSION['country']===$_country['value']) echo "selected"; ?>>

echo the session value(if set) to the hidden field too

arun
  • 3,667
  • 3
  • 29
  • 54