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>
$counterevery time form is posted?$_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?<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??