我有以下代码=>
<button class="btn btn-lg btn-warning text-center but-width" id="first" name="geri" type="submit" value="<?php ?>">BACK</button>
<button class="btn btn-lg btn-warning text-center but-width" id="second" name="ileri" type="submit" value="<?php ?>">FORWARD</button>
这些按钮将表单提交到同一页面。在页面顶部,我有变量$t,它被初始化为值"0",后面跟着代码=>
$t=0;
if( $this->input->post('ileri') ) { $t=$t+1;} // Back button was pressed;
if( $this->input->post('geri') ) { $t=$t-1;} // Forward button was pressed;
现在,当我按下forward时,我想增加$t,而当我按下back时,我又想减少它。但由于顶部的声明($t=0;),它可以用"forward"按钮达到"1",用"back"按钮回到"0"。有人能想出解决这个问题的办法吗。非常感谢。
添加一个隐藏输入,并将$t存储在那里。这将在提交时发布:
<?php
$t = $this->input->post('t');
if (!$t) $t = 0;
if( $this->input->post('ileri') ) { $t=$t+1;} // Back button was pressed;
if( $this->input->post('geri') ) { $t=$t-1;} // Forward button was pressed;
?>
<input name="t" type="hidden" value="<?=$t ?>">
<button class="btn btn-lg btn-warning text-center but-width" id="first" name="geri" type="submit" value="submit">BACK</button>
<button class="btn btn-lg btn-warning text-center but-width" id="second" name="ileri" type="submit" value="submit">FORWARD</button>
您可以使用url参数。获取而不是发布。从而存储变量$t的当前值,即
if (isset($_GET['t']))
{
$t = $_GET['t'];
}
else
{
$t = 0;
}
这样的东西可能会对有所帮助
如果您想知道提交的按钮是什么,您需要更改按钮
<button class="btn btn-lg btn-warning text-center but-width" id="first" name="geri" type="submit" value="<?php ?>">BACK</button>
<button class="btn btn-lg btn-warning text-center but-width" id="second" name="ileri" type="submit" value="<?php ?>">FORWARD</button>
至
<button class="btn btn-lg btn-warning text-center but-width" id="first" name="s_button" type="submit" value="geri">BACK</button>
<button class="btn btn-lg btn-warning text-center but-width" id="second" name="s_button" type="submit" value="ileri">FORWARD</button>
在您的控制器中
$button_s = $this->input->post('s_button');
if( isset($button_s)){
switch($button_s){
case 'geri':
//do somthing
break;
case 'ileri':
//do somthing
break;
}
}