Error "Invalid argument supplied for foreach()" and "Undefined index" in PHP form

Asked

Viewed 328 times

-4

I must create 20 inputs that when filled out should show what was typed in them right below, using arrays and foreach, here is the code below:

function repeteInput(){
    for($i = 1; $i <= 20; $i++){
        echo '<form action="repeteInput.php" method="post">';
        echo 'Digite um número aqui ('.$i.')<input type="text" name "produto[]"><br>';
    }
    echo '<input type="submit" value="Enviar"></form>';
    $produtos = $_POST['produto'];
    foreach ($produtos as $produto){
        echo $produto."<br>";
    }
}

echo repeteInput();

I already checked the code, and it looks exactly like the one I copied in my notebook, which worked on another PC. Even so, it shows this error after the page:

Notice: Undefined index: produto in C:\xampp\htdocs\24.04.2017\repeteInput.php on line 8

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\24.04.2017\repeteInput.php on line 9
  • First of all, put the <form action="repeteInput.php" method="post"> out of the loop for, for various tags <form> are being opened and there is only one tag closing.

  • The same mistakes keep popping up

  • Yes, I know, but it’s just to improve your code. After all, it’s a comment, not an answer.

  • and post the HTML part because the problem is in it.

1 answer

1

In this case $product = $_POST['product']; not receiving an array, try this

<form action="" method="POST">
    <input type="text" name="test1">
    <input type="text" name="test2">
    <input type="text" name="test3">
    <input type="text" name="test4">
    <input type="text" name="test5">

    <button type="submit">Enviar</button>
</form>

<?php
    foreach ($_POST as $produto) {
        echo $produto . "<br>";
    }
?>

Considering your need, I suggest something like this: Note. Understand that this is just an example to help you think, in production there are better ways to do what you need.

<form action="" method="POST">
    <?php
        for($i = 1; $i <= 20; $i++)
        {
            echo "<label>Produto $i: </label>";
            echo "<input type='text' name='$i' value=''><br>";
        }

    ?>
    <button type="submit">Enviar</button>
    <hr>
</form>

<?php
    foreach ($_POST as $key => $produto) {
        echo "Nº: " . $key. " - Produto: " .$produto . "<br>";
    }
?>

Code having the same effect

<form action="" method="POST">
    <?php
        for($i = 1; $i <= 20; $i++)
        {
            echo "<label>Produto $i: ";
            echo "<input type='text' name='".$produto[] = $i."' value=''><br>";
        }

    ?>
    <button type="submit">Enviar</button>
    <hr>
</form>

<?php
    foreach ($_POST as $key => $produto) {
        echo "Nº: " . $key. " - Produto: " .$produto. "<br>";
    }
?>
  • Apart from the semantic errors of the question, its solution is still better, because in fact, $produtos gets a array yes, because he defined the input as name="produtos[]". The error was caused by trying to access a variable in $_POST without validating whether there was a request POST. In the solution presented here, if there are more fields in the form, it is already unfeasible, even if using <input type="submit">, as the same is sent along with the request.

  • So, as quoted, the initial idea was to present a basis for him to follow. How was said the expression - $products = $_POST['product']; - does not return an array, but input produced by it in - name "product[]" - is in fact an array, so much so that I presented in the third idea a solution using its method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.