doufu2496 2016-04-15 11:05
浏览 100
已采纳

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

I have the following code=>

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

These buttons submit the form to the same page. At the top of the page I have variable $t which is initialized at value '0' and is followed by the codes=>

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

Now as I press forward I want to increase $t and when I press back I want to decrease it. But because of the declaration at the top ($t=0;) it goes as far as '1' with 'forward' and as back as '0' with 'back' buttons. Can anyone think of a way of getting over this problem. Thank you...

  • 写回答

3条回答 默认 最新

  • dsfbnhc4373 2016-04-15 11:16
    关注

    Add a hidden input, and store $t there. This gets posted upon submit:

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部