The problem: Write a function that puts a list of items from a form into an array (text field) using PhP/HTML.
Tools to use: PhP and HTML.
So I have been looking around a lot to find a solution to this problem but so far have been unsuccessful. Essentially I want a text field for user input and a submit button. Once the text has been submitted it is added to an array which consists of all previously entered text. After digging around it seems that 2 possible solutions to this problem would be to create PhP Sessions or the use of Hidden fields. I have minimal to no knowledge of how to implement Sessions or hidden fields.
Here is the code I wrote so far(trying many different variations), but I still end up with the same results, the newest text overwrites the existing array so the output is always "Array[0] => string". Any help in learning this would be awesome.
<?php
$item = [];
function itemList() {
if(isset($_POST['string'])) {
$pieces = explode(" ", $_POST['string']);
$item[$pieces] = $_POST['item'];
return $item[$pieces];
}
if(isset($_POST['item'])) {
foreach($_POST['item'] as $key => $pieces) {
foreach($pieces as $key => $piece) {
$item[$pieces] = $piece;
}
}
}
return "<input type='hidden' name='item' value='<?php $_POST['item']; ?>' />";
}
?>
<html>
<head>
</head>
<body>
<form method="POST">
<input type="text" name="string" />
<input type="submit" value="Submit" />
<input type="hidden" name="item" value="<?php $_POST['item']; ?>" />
</form>
<h1><?php itemList(); ?></h1>
</body>
</html>
</div>