【问题标题】:increment numaric (integer) variable by clicking a button in php通过单击 php 中的按钮来增加数字(整数)变量
【发布时间】:2014-09-20 17:46:21
【问题描述】:

当我使用 PHP 和 HTML 单击按钮时,我需要将 PHP 中的整数变量加减一。

我不应该在这个场景中插入任何数字。

<HTML> <input type = "submit" name = "incr" value = '1'/> </HTML>

我可以使用这个代码吗?

<?php $raw = $_POST['incr']+$raw; ?>

每次我点击按钮时,$raw 应该增加一,如果我点击减少按钮,它应该减少一。

谢谢。

【问题讨论】:

  • 您是否将值保存在 db 中?还是只是想展示它?
  • 如果你想保存到 db 使用 jQuery + ajax
  • 变量应该在 php 中使用。它不会保存到数据库中。

标签: php button click add


【解决方案1】:

使用会话

<?php
session_start();
$_SESSION['raw'] += intval($_POST['incr']);

?>

另外,如果你想减少 $_SESSION['raw'],只需添加一个简单的 if-else 和 POST 变量。

【讨论】:

    【解决方案2】:

    使用以下

    <?php
    $raw =$raw+intval($_POST['incr']);
    
    ?>
    

    【讨论】:

      【解决方案3】:
      <?php
      print_r($_GET);
      if(isset($_GET['current_value']))
      {
      if(isset($_GET['valinc']))
      {   
      $current_value=$_GET['current_value']+1;    
      }else{
      $current_value=$_GET['current_value']-1;        
      }
      }else{
      $current_value=0;   
      }
      ?>
      <form name="form1" method="get">
      <input type="text" name="current_value" value="<?php echo $current_value; ?>"/>
      <input type="submit" name="valinc" value="+1" />
      <input type="submit" name="valdec" value="-1" />
      </form>
      

      检查这个简单的 php 表单脚本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-31
        • 2019-08-21
        • 1970-01-01
        • 2014-12-09
        • 1970-01-01
        • 2019-08-03
        相关资源
        最近更新 更多