如何通过按下提交按钮PHP来添加(或减去)一个值

时间:2016-04-15 19:05:15

标签: php twitter-bootstrap codeigniter

我有以下代码=>

<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,它被初始化为值&#39; 0&#39;然后是代码=&gt;

     $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;

现在,当我向前推进时,我想增加$ t,当我按下时我想减少它。但由于顶部的声明($ t = 0;),它远远超过了&#39; 1&#39;与&#39;前进&#39;并且回到&#39; 0&#39;与&#39;回来&#39;纽扣。任何人都可以想到克服这个问题的方法。 谢谢......

3 个答案:

答案 0 :(得分:2)

添加隐藏的输入,并在那里存储 $ 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>

答案 1 :(得分:1)

您可以使用url参数。获取而不是发布。这样它就可以存储变量$ t的当前值,即

if (isset($_GET['t']))

{
 $t = $_GET['t'];
}
else
{
$t = 0;
}

这样的事情可能会有所帮助

答案 2 :(得分:1)

如果您想知道提交哪个按钮,您需要更改按钮

<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;
    }
}