简体   繁体   English

需要隐藏的输入值以保留表格php

[英]Need hidden input value to stay in form php

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. 我正在使用隐藏字段来用php提交选择,当我点击“提交”按钮时,我得到了选择的国家。 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: 借助JS分配的隐藏输入字段的“值”:

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

Thank you for your help. 谢谢您的帮助。

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 . 当您将会话用于其他目的时,请根据其自身的有效期分别设置cookie。 using setcookie . 使用setcookie

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. 或者,您可以使用会话cookie将值传播到其他页面。

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 echo将会话值(如果设置)回显到隐藏字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM