我正在使用$_POST
将数据发布回字段,然后再对其进行操作。但是,功能不是我想要的。
// Default to current date
if(!isset($_POST['inputText3'])) {
$input = '2014';
} else {
// grab input
$input = $_POST['inputText3'];
}
// manipulation to run each time
$input = $input + 1;
if(isset($_POST['year1']) || isset($_POST['year2'])) {
// Do more complex stuff - but simplified (duplicated) below for this example
echo '<form method="post" action="">';
echo '<button type="submit" name="year1" value="2016" class="button">2016</button>';
echo '<button type="submit" name="year2" value="2017" class="button">2017</button>';
echo '<input type="text" name="inputText3" value="' . $input . '">';
echo '</form>';
} else {
echo '<form method="post" action="">';
echo '<button type="submit" name="year1" value="2016" class="button">2016</button>';
echo '<button type="submit" name="year2" value="2017" class="button">2017</button>';
echo '<input type="text" name="inputText3" value="' . $input . '">';
echo '</form>';
}
最初在第一次页面加载时,我的函数从input3
(2014)中获取一个初始值,对其进行操作(+1),然后将其输出回输入字段(2015)。这就是我想要的。
接下来我想发生的是,当单击一个按钮(由 PHP 生成)时,再次执行相同的操作。
相反,在单击按钮时,页面会重新加载并从input3
(2014) 中获取原始值 - 尽管操纵的值 (2015) 在input3
中可见。
这意味着我的页面有点击"延迟"。我认为问题是错误地使用了下面的代码:
if(!isset($_POST['inputText3'])) {
$input = '2014';
} else {
//get posts date
$input = $_POST['inputText3'];
}
会话是一种选择吗?发布到同一页面时可以使用它们吗?
现在你更清楚了,重新阅读你的问题;试试这个——
<?php
$input = (isset($_POST['lastYear'])) ? "$_POST[lastYear]" : '2014';
if (isset($_POST['year']) AND $_POST['year'] == 1) {
$input++;
} elseif (isset($_POST['year']) AND $_POST['year'] == 2) {
$input += 2;
}
echo $input;
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<input type="radio" name="year" value="1"> - Add 1 year<Br>
<input type="radio" name="year" value="2"> - Add 2 years<Br>
<input type="hidden" value="<?php echo $input ?>" name="lastYear">
<input type="submit" name="submit">
</form>
编辑:Whoops有2013年而不是2014年:)
=
我认为这是个问题..应该有==
。
您通过分配=
来赋予价值。
如果要比较(三元运算符),则应使用 ==
$cur_year= 2014;
if(!isset($_POST['inputText3'])) {
$input = ($cur_year=='2014')?$_POST['inputText3'] :'2014';
} else {
//get posts date
$input == isset($_POST['inputText3']) ?$_POST['inputText3'] :'';
}