1

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.

4
  • 1
    Please explain what you are trying to achieve, in a broad sense, because this code is pretty illogical Commented Dec 1, 2015 at 13:50
  • sorry, what I'm trying to do is : 1. Post a form 2. have the user enter a value 3. store that value into an array 4. repeat that 10 times (in this case) 5. print all those values one by one I know there are easier ways to that but this is a part of a bigger problem thanks for quick respons. Commented Dec 1, 2015 at 13:55
  • php is stateless - it runs untill the end of the script, then stops, destroying any memory used during that execution. By time you see the html, php has finished. Your code suggests you expect the code to somehow pause after printing each form, then resume on submit, thats not how it works im afraid Commented Dec 1, 2015 at 13:59
  • Okay, thank you. As you can tell I'm quite new haha thanks for a quick respons though Commented Dec 1, 2015 at 14:01

1 Answer 1

1

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']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.