【问题标题】:Array and For each Function Not Working数组和每个函数不起作用
【发布时间】:2015-11-21 22:21:35
【问题描述】:

请有人帮助我,我正在尝试使用数组函数根据复选框的结果为整体变量赋予正值或负值。

复选框的问题是,存在以下哪种驯鹿,所以对于每一个正确的驯鹿,我希望给变量 $finalvalue 一个增量值。然后在最后留下一个 IF 函数来说明是否勾选了错误的驯鹿,给出一个减量值,以及正确的增量值。

我附上了下面的代码,每次我使用它时,该值都不会按预期增加,或者不是每个正确驯鹿的累积值。

谢谢。

HTML 优先

Which of the following are of Santa's Reindeer?(You can select more than one answer)<br>

<input type="checkbox" name="reindeer" value="Rudolph">Rudolph<br>
<input type="checkbox" name="reindeer" value="Prancer">Prancer<br>
<input type="checkbox" name="reindeer" value="Dancer">Dancer<br>
<input type="checkbox" name="reindeer" value="Ronald">Ronald<br>

<br><br>
<input type="submit" value="Submit">

PHP 脚本接收端

$reindeer=$_GET['reindeer'];

$type=array("Rudolph","Dancer","Prancer");
foreach($type as $reindeer){$finalvalue = $finalvalue+2;};

if ($reindeer=="Donald"){$finalvalue = $finalvalue-6;}


print "$finalvalue";

感谢您的帮助。

【问题讨论】:

  • if, 应该在foreach里面吗?

标签: php arrays variables foreach get


【解决方案1】:
<?php

    $input = $_GET['reindeer'];
    $valid = ["Rudolph","Dancer","Prancer"];

    # Debug input..
    echo 'Raw input data:<pre>';
    var_dump($input);
    echo '</pre>';


    if(is_array($input)){
        foreach($input as $reindeer){
            if(in_array($reindeer, $valid)){
                echo 'user supplied multiple answers and '.$reindeer.' is listed in $valid. </br>';
                $finalvalue = $finalvalue+2;
            } else {
                echo 'user supplied multiple answers and '.$reindeer.' is listed <strong>not</strong> in $valid. </br>';
                $finalvalue = $finalvalue-6;
            }
        }
    } else {
        # This code should never execute unless the user changes the html code.
        if(in_array($input, $valid)){
            echo 'user supplied 1 answer and '.$reindeer.' is listed in $valid. </br>';
            $finalvalue = 2;
        } else {
            echo 'user supplied 1 answer and '.$reindeer.' is listed <strong>not</strong> in $valid. </br>';
            $finalvalue = 0;
        }
    }


    echo $finalvalue;

?>

【讨论】:

  • 不幸的是,这个答案给我返回了以下错误命令,您还有其他建议吗?警告:在第 58 行的 /Applications/XAMPP/xamppfiles/htdocs/php_class/PHP Project/naughtyornice.php 中为 foreach() 提供的参数无效
  • 表示输入不是数组。我已经更新了代码来弥补这一点。
  • 非常感谢您的帮助,您是正确的,它是一个字符串,我将来如何适应这个?我可以简单地输入 ///$type=string("rudolph","dancer","pracer") 等吗?非常感谢
  • 在您的 HTML 代码中,将 name="reindeer" 更改为 name="reindeer[]"
【解决方案2】:
Which of the following are of Santa's Reindeer?(You can select more than one answer)<br>

<input type="checkbox" name="reindeer[]" value="Rudolph">Rudolph<br>
<input type="checkbox" name="reindeer[]" value="Prancer">Prancer<br>
<input type="checkbox" name="reindeer[]" value="Dancer">Dancer<br>
<input type="checkbox" name="reindeer[]" value="Ronald">Ronald<br>

<br><br>
<input type="submit" value="Submit">



 $input = $_GET['reindeer'];
 $valid = ["Rudolph","Dancer","Prancer"];


 foreach($input as $reindeer){
      if(in_array($reindeer, $valid)){
            $finalvalue = $finalvalue+2;
       } else {
            $finalvalue = $finalvalue-6;
       }
  }

注意 html 中的括号。将这些括号添加到输入的名称使其成为一个数组。因此,生成的代码更简洁、更少。

【讨论】:

  • 是的,但是用户可以更改 html 代码并发布答案,使其显示您不想显示的关键信息。始终清理用户输入。
  • 你想举个例子吗?他的用例非常简单
  • 使用 Firefox 或 Chrome,检查元素,删除名称中的括号,然后提交结果。即使他的案例非常简单,最好以正确的方式学习而不是通过艰难的方式学习。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 2016-02-18
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
相关资源
最近更新 更多