Using filter_input_array (INPUT_POST, "var_name") vs. $ _POST [closed]

1

I have the following input variable in my form:

<input class="caption_text" type="text" name="caption[]" required/>

That I create dynamically with JS.

After submitting I test the following:

$mycaption = \filter_input_array(\INPUT_POST, 'caption');
foreach ($mycaption as $eachInput) {
    echo "Caption " . $eachInput . "<br>";
}

I discovered that the above code did not work.

However, typing $ _POST, like this:

$cpost = $_POST["caption"];

foreach ($cpost as $eachInput) {
    echo "Caption " . $count. " - " . $eachInput . "<br>";
}

Then it works as expected.

Can anyone please tell me why the first approach does not work?

    
asked by anonymous 30.07.2016 / 18:35

1 answer

0

I did a test simulating the code that I posted and formulated the code below using the function:

<?php
$filtro = array( 
    'caption' => array(
        # aqui pode variar de acordo com o valor esperado do seu input
        'filter' => FILTER_VALIDATE_STRING,
        'flags' => FILTER_REQUIRE_ARRAY
    )
);
$form = filter_input_array(INPUT_POST, $filtro); 
if (array_key_exists("caption", $form))
{
    foreach($form['caption'] ...)
    {
         # ...
    }
}

Output

<?php
var_dump($form);
# array(1) { ["caption"]=> array(2) { [0]=> int(2) [1]=> int(3) } }
    
30.07.2016 / 19:23