0

I am trying to create a simple form which will allow me to add the values from the form to the a list or array and then display them on the same page.

I also need to be able to keep adding to this list on the same page and not loose the previously added values.

At present when I add something using the following method it displays fine on the page but if I add another one it overwrites what is already there.

$support_list = array();
            echo "<form method='post' name='support'>";
            echo "<textarea name='support' rows='6' cols='69'></textarea>";
            echo "<input type='submit' name='s_submit'>";
            echo "</form>";

            if($_POST['s_submit']) {
                $s=array("test7"=>$_POST['support']);
                array_push($support_list,$s);
            }
print_r($support_list);

Here is the output on the page.

enter image description here

Ideally I would want something like this, and each time you click the submit button it keeps adding to the array, the end goal is to use the values in that array to write to a DB instead of doing it one at a time.

Array ( [0] => Array ( [test7] => Hello Test Adding ), [1] => Array ( [test8] => Hello Test Adding 1 ) )
  • You either need to store the array in a session, or embed it in the form as a hidden input so it basically gets send back end forth from the server to the client and back and is preserved that way… – deceze Nov 30 '21 at 08:07
  • @deceze This.... This is what I needed why did I not think to use the Session!! – Danny Broadhurst Nov 30 '21 at 08:13
  • Frankly I'd rather recommend the hidden form input, unless those arrays can get really huge or you have some other reason to use a session. That way the server remains stateless (doesn't need to store any data), which is usually desirable. – deceze Nov 30 '21 at 08:23

0 Answers0