David Erixon
David Erixon

Reputation: 11

Enter values through a form into array, PHP

I'm trying to have a number of forms with one field each and make the input enter in to the same array.

This is my code:

<?php
$parts = array();

for($i = 0; $i < "10"; $i++)
{
    echo '<form action="index.php" method="post">';
    echo '<input type="text" name="parts[]"><br>';
    echo '<input type="submit">';
    echo '</form>';
    $parts[$i] =  $_POST['holder'];
    unset($_POST['holder']);
}

$arrlength = count($parts);
for($i = 0; $i < $arrlength; $i++) {
    echo $parts[$i];
    echo "<br>";
}


?>

As of right now the number I choose at random was 10 it's supposed to be any given number by the user, but this is just for test purposes.

The problem I'm having is that it only posts the last part, I've tried a bunch of different ways but none have been successful so far.

Upvotes: 1

Views: 136

Answers (1)

Charles Bryant
Charles Bryant

Reputation: 1015

It sounds like you want to submit a form with multiple entries in an array?

You would need to do it something like:

echo '<form action="index.php" method="post">';
for($i = 0; $i < "10"; $i++)
{

    echo '<input type="text" name="parts['.$x.']"><br>'; 
}
echo '<input type="submit">';
echo '</form>';

Then in the code that you are posting to

var_dump($_POST['parts']);

Upvotes: 1

Related Questions