[英]Remembering php form data to display currency all over a website
I've set up a currency conversion dropdown in a wordpress site. 我已经在wordpress网站上设置了货币换算下拉菜单。 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. 我尝试了在这里看到的建议的js cookie,但是它不起作用。
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: 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: 我也将$ _SESSION添加到表单中:
<option value="0" <?php if($_SESSION['sort'] == "0") echo "selected";?>>US Dollars (USD)</option>
UPDATE 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. 我会将所选的值存储在$_SESSION['selected_currency']
变量和交叉检查中,并在使用货币列表填充下拉列表时将其选中。
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. 可能会有更短的方法,我将其用于说明目的。
对于永久保留数据,您只有几种可能性,最容易实现的是$ _SESSION,$ _ COOKIE或在数据库中。
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');
第一种是在options.php
页面中添加一个字段并保存数据,然后从options.php
获取数据,因为您必须使用update_option('nameOfField_form','nameOfFieldDb');
and get_option('nameOfFieldDb')
. 和get_option('nameOfFieldDb')
。
and 2nd option is by jquery.ajax();
第二个选项是通过jquery.ajax();
method save your data in options.php
方法将您的数据保存在options.php
you may find these links helpful codex 您可能会发现这些链接对法典很有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.