1

I am trying to create a quiz, in order to do this I am using a form which users will input information into, once they submit this form it should add the input to an array/list.

The user should then be able to enter information and the process would repeat.

The finished product would be an array with each element corresponding to the order the answers were given.

-

I have tried so far using both array_push() and declaring elements, eg: $my_array[0] = $input;.

The current problem I am experiencing is that each time I submit the form, the $count variable doesn't seem to increment.

Instead it simply stores the data in the first element and overwrites which was previously there.

I am inclined to believe this is a problem with the posting of the submit button.

-

Here is my code:

<html>
    <body>
        <form action="" method="POST">

            <input type="text" name="INPUT" placeholder="Input something"; required /><br><br>

            <input type="submit" name="Submit" /><br><br>

<?PHP 
    $my_array = array();

    $count = 0;

    if(isset($_POST['Submit'])){

        global $count;
        $input = $_POST['INPUT'];

        $my_array[$count] = $input;

        print_r($my_array);
        echo "Count:" . $count;

        $count++;
    }

?>  
        </form>
    </body>         
</html>
8
  • Are you wanting to increment $counter every time form is posted? Commented Oct 10, 2019 at 22:00
  • It's not overwriting anything because you're not storing it; you're just printing whatever's present in $_POST['INPUT']. You could look into storing it in cookies or local storage, but the idea of submitting/reloading a page several times doesn't sound like good UX. Why can't you display all the questions at once? Commented Oct 10, 2019 at 22:01
  • @user7290573 If i display all of the questions at once through a loop the "name" in the form will all be the same thing within that loop and so I won't be able to retrieve the unique answers the user inputs into each. Commented Oct 10, 2019 at 22:05
  • 2
    you only have one input element in your form and one input variable in $_POST, so a loop makes no sense. Web applications are stateless - the value of your variables does not persist from one request to another (a new request is made every time you submit the form from a browser, or click on a link). To persist values between requests you must use some storage e.g. file, database or even just the session, and retrieve the store the variables each time. Commented Oct 10, 2019 at 22:14
  • 1
    "If i display all of the questions at once through a loop the "name" in the form will all be the same thing within that loop and so I won't be able to retrieve the unique answers" ...true, unless you use array syntax e.g. <input type="text" name="INPUT[]" placeholder="Question 1"; required /><input type="text" name="INPUT[]" placeholder="Question 2"; required /> ...and then $_POST["INPUT"] would contain an array of data when you submit the form. Or...just give each question field a unique "name" attribute - how do you think forms with multiple fields work in most websites?? Commented Oct 10, 2019 at 22:16

1 Answer 1

3

The crux of the issue here is that variable values do not persist across PHP requests. Every time you submit the form, you are throwing away your old $count and $my_array variables and initializing new variables with the same names.

Here is a working version of your code snippet, which takes advantage of the PHP $_SESSION variable to have persistent information between requests:

<?php
session_start();
if (!isset($_SESSION["my_array"])) {
    $_SESSION["my_array"] = array();
}
?>

<html>
    <body>
        <form action="" method="POST">
            <input type="text" name="INPUT" placeholder="Input something"; required /><br><br>
            <input type="submit" name="Submit" /><br><br>

            <?php
                if(isset($_POST['Submit'])){
                    array_push($_SESSION["my_array"], $_POST['INPUT']);

                    print_r($_SESSION["my_array"]);
                    echo "Count:" . count($_SESSION["my_array"]);
                }
            ?>
        </form>
    </body>         
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

That is one way to skin the cat. Or you could pass the values in the POST. The scope of the variables is indeed the issue. Keep in mind that PHP is all one string Kieran.
@Ususipse Thank you, I will keep that in mind in the future.

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.