0

So basically what I'm doing is generating a simple form to take in a number. Based on this number I'm producing an equal normal of text inputs with a new submit button associated for these input forms. The problem is I need to keep up with the original number of forms to pass as an index value to an array that I want to store the values from the new set of text inputs. But when I submit the new form, the value keeps dropping. Here is my code below.

Note: I've commented out the algorithm I'm attempting to store the values from the text inputs and just attempting to echo the value number of forms and not having this value is preventing me from entering my for loop where I'd like to store the values.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
    <title>Kid Order Selector</title>
</head>
<body>
    <form action="kid_selector.php" method="post">
        Enter number of kids playing:<input type="text" name="num_kids" /><input type="submit" name="submit" value="Submit" />
    </form>
    <br /><br />
    <form action="kid_selector.php" method="post">
        <?php

            if(isset($_POST["num_kids"])) {
                $num_kids = $_POST["num_kids"];
                echo "Enter the following kids names:<br />";
                for($i = 1; $i <= $num_kids; $i++) {
                    echo "<input type=\"text\" name=\"kid{$i}\"  /><br />";
                    $index = $i;
                }

                if( $num_kids == $index ) {
                    echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />";
                }

                $numkids = $num_kids;
            } else {
                $num_kids = "";
            }
        ?>

        <?php

            //$kids = array();
            echo $numkids;

            if(isset($_POST["kid1"])) {
                echo "Success";
                for($i=0; $i < $numkids; $i++) {
                   // $index = $i + 1;
                    //$pos = "kid{$index}";
                    //echo $pos;
                    //$kids[$i] = $_POST[$pos];
                    echo "Success";
                }

                //print_r($kids);
            }
        ?>    


    </form>


</body>
</html>
0

1 Answer 1

1

When I submit the new form, the value keeps dropping

Yes, that will happen. Each request of a PHP script starts with fresh variables. Data is not remembered unless you store it somewhere persistent, such as in a database, a file, a cookie, in session storage, etc.

To preserve the num_kids value, you could write it as a hidden input of the second form:

if(isset($_POST["num_kids"])) {
    $num_kids = $_POST["num_kids"];
    echo '<input type=hidden name=num_kids value=' . $num_kids . '>';

Then, when the second form is submitted, you can read that value back from $_POST a second time.

An alternative way to do your form would be with an array variable as the input name. So:

echo "Enter the following kids names:<br />";
echo str_repeat('<input name="kids[]"><br />', $num_kids);

Then when the second form is submitted, you can read the array directly from $_POST without having to build it using a loop.

if (isset($_POST['kids'])) {
    echo 'Success.<br />';
    print_r($_POST['kids']);
}

Then if you need the num_kids value, it is count($_POST['kids']).

1
  • I like solution 2 over the first solution you mentioned. Though I did run into some errors implementing the str_repeat, so I looked in the PHP.net documentation and found out that the parameters you listed were reversed. It should actually be set as the following: string str_repeat ( string $input , int $multiplier ) Oct 14, 2013 at 18:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.