【问题标题】:PHP: Cannot add numerical values from a FORM POSTPHP:无法从 FORM POST 添加数值
【发布时间】:2014-02-07 14:19:27
【问题描述】:

我有一个表格:

 <form name="myform" action="" method="post">
 Unilateral lower limb pain:
 <select name="pain">
     <option value="1">yes</option>
     <option value="0">no</option>
 </select>
 <input type="submit" name="submit" value="submit" />
 </form>

我正在尝试向 var $_POST[pain] 添加值,但结果始终为 0

 <?php
 if (isset($_POST['submit'])){

 $thescore=0;

 $_POST['pain']=intval($painScore);

 if($painScore==1){
  $thescore=$thescore+3; 
 }

 echo 'score is: '.$thescore;

 }
 ?>

如果在应该输出 1 时选择“yes”作为选项,则输出“score is: 0”

我也试过了:

  int()$_POST[pain]=$painscore;

结果相同,输出为0。

当从 FORM 中检索时,如何将数字(浮点数和整数)相加?

【问题讨论】:

  • $_POST[pain] 应该是 $_POST['pain']
  • $_POST 是一个超全局变量,包含传递给 HTTP 正文中脚本的数据(例如,从带有 method="post" 的
    ) - 你不能真正“写入”到 @987654325 @ 在运行时以您尝试的方式。

标签: php forms add


【解决方案1】:
<?php

if (!empty($_POST)) {
    $score = filter_input(INPUT_POST, "pain", FILTER_VALIDATE_INT, array(
        "options" => array(
            "default"   => 0,
            "min_range" => 0,
            "max_range" => 1,
        )
    ));
    if ($score === 1) {
        $score += 3;
    }
    echo "Score is: {$score}";
}

?>

【讨论】:

  • ^ 真的比我的回答好 ;) +1
【解决方案2】:

这段代码没有任何意义

<?php
 if (isset($_POST['submit'])){

 $thescore=0;

 $_POST[pain]=intval($painScore);

 if($painScore==1){
  $thescore=$thescore+3; 
 }

 echo 'score is: '.$thescore;

 }
 ?>

我想你需要这样做

<?php
 if (isset($_POST['submit'])){

 $thescore=0;
 $painScore = (int)$_POST['pain'] ;

 if($painScore==1){
  $thescore=$thescore+3; 
 }

 echo 'score is: '.$thescore;

 }
 ?>

【讨论】:

    【解决方案3】:

    你的语法错误

    $_POST[pain]=intval($painScore);

    必须

    $painScore=intval($_POST[pain]);

    【讨论】:

      【解决方案4】:
      $painScore=intval($_POST['pain']);
      

      【讨论】:

        【解决方案5】:

        $_POST 是一个超全局变量,包含传递给 HTTP 正文中脚本的数据(例如,从带有 method="post"&lt;form&gt; ) - 你不能像你在运行时那样真正“写入”到 $_POST '正在尝试。

        假设我理解正确(并且可能实际可行),一种更简洁的方式来编写您正在尝试的内容是:

        // variable definition
        $iPainScore = 0;
        
        // evaluation
        if(!empty($_POST)) {
        
          // increment $iPainScore by the value of $_POST['pain']
          // unless $_POST['pain'] == 1, in which case increment $iPainScore by 3
          $iPainScore += $_POST['pain'] == 1 ? 3 : (int) $_POST['pain'];
        }
        
        // output
        echo "The score is {$iPainScore}";
        

        编辑

        ...实际上,由于 $_POST['pain'] 只能是 0 或 1,因此您根本不需要将其添加到您的疼痛评分中;您似乎使用 $_POST['pain'] 作为布尔值,如果 $_POST['pain'] 为“真”(对于 (int) 1 == (bool) true),则实际上将您的疼痛评分增加 3。

        因此,您可以将整个事情(假设您从疼痛评分 0 开始,而不是增加现有的疼痛评分)减少到:

        $iPainScore = !empty($_POST['pain']) ? 3 : 0;
        echo "The score is{$iPainScore}"'
        

        【讨论】:

          猜你喜欢
          • 2019-03-23
          • 1970-01-01
          • 1970-01-01
          • 2020-05-19
          • 1970-01-01
          • 2016-04-06
          • 2014-07-19
          • 2017-02-27
          • 1970-01-01
          相关资源
          最近更新 更多