【问题标题】:PHP: Obtaining data from input in HTML form then output using filter_arrayPHP:从 HTML 表单的输入中获取数据,然后使用 filter_array 输出
【发布时间】:2016-09-19 02:49:16
【问题描述】:

我基本上正在尝试创建一个过滤系统,它从 HTML 表单中的输入中获取数据,然后在 PHP 中使用该数据过滤来自 $profileArray 的现有“配置文件”,输出包含用户输入的数据/变量的任何配置文件(s)。我不知道如何通过我的 filter_array 函数发送用户输入数据,据我所知,这些函数可以使用var_dump 对其进行测试。

作为一个初学者,我无法理解 php.net 中的 $foo 和 $bar 解释,因此非常感谢清晰的解释。

HTML

<html>
<body>
    <form action="profileFilter.php" method="POST">
    <p>Age: <input type="text" name="ageChoice"></p>
    <p>Colour: <input type="text" name="colourChoice"></p>
    <input type="submit" name="catQualitiesBtn">   
    </form>
</body>
</html>

PHP

<?php    
    $profileArray = array( 
    array(  'Name' => "Toby",
            'Age' => 3, 
            'Colour' => "Ginger",
            ),

array(  'Name' => "Cassie",
        'Age' => 3, 
        'Colour' => "Tabby",
        ),

array(  'Name' => "Lucy",
        'Age' => 1, 
        'Colour' => "Black",
        )
        );

function get_profile_by_age ($profile, $age){
    return array_filter ($profile, function($ageInput) use ($age){
    return $ageInput['Age'] === $age;
   });    
   }

 function get_profile_by_age ($profile, $colour){
    return array_filter ($profile, function($colourInput) use ($colour){
    return $colourInput['Colour'] === $colour;
  });    
  }

$ageInput = intval($_POST['ageChoice']);
$colourInput = strval($_POST['colourChoice']);

echo function get_profile_by_age ($ageInput , $age);
echo function get_profile_by_colour ($colourInput , $colour);
?>

【问题讨论】:

    标签: php html filter


    【解决方案1】:

    我不明白你的问题是什么,但我认为你无法遍历你的数组。这个循环也将帮助您循环遍历多维数组。而且在您的数组中,年龄不在字符串中,因此您不必将用户输入的值从字符串更改为 int。

    <html>
    <body>
    <form action="profileFilter.php" method="POST">
    <p>Age: <input type="text" name="ageChoice"></p>
    <p>Colour: <input type="text" name="colourChoice"></p>
    <input type="submit" name="catQualitiesBtn">   
    </form>
    </body>
    </html>
    

    PHP

    <?php
    
     $profileArray = array( 
    array(  'Name' => "Toby",
            'Age' => 3, 
            'Colour' => "Ginger",
            ),
    
       array(  'Name' => "Cassie",
        'Age' => 3, 
        'Colour' => "Tabby",
        ),
    
      array(  'Name' => "Lucy",
        'Age' => 1, 
        'Colour' => "Black",
        )
        );
    
      if (isset($_POST['ageChoice']) && isset($_POST['colourChoice'])) {
       $ageInput = $_POST['ageChoice'];
       $colourInput = $_POST['colourChoice'];
    
     for ($i=0; $i >=0 ; $i++) { 
            if ($profileArray[i]['Age'] == $_POST['ageChoice'] && $profileArray[i]['Colour'] == $_POST['colourChoice']) {
                echo "name -".$profileArray[i]['Name'];
                break;
            }
        }
     }
    }
    

    【讨论】:

    • 感谢intval 的负责人,我实际上在您发送答案之前对其进行了编辑。我没有收到错误,但在将值提交到 HTML 表单后没有显示任何内容?我正在尝试制作一个过滤系统,基本上可以从$profileArray 吐回包含在 HTML 表单中输入的字符串数据的配置文件。
    • @eainnem 使用上述循环
    • 我做了,但它不起作用。它让我进入一个连续的循环。最后你还有一个额外的 }....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 2016-12-23
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多