You can do this two ways. Both are suitable. I think I would consider the second way more secure as it is done completely server side. I will demo both for you.
First way:
You are going to use a hidden input field and serialize an array. You will pass the serialized array back to your post array on submit via the hidden input field. The code will push the new post data onto the unserialized array that it got from the hidden input field.
Like so:
<?php
if(isset($_POST['submit']) && $_POST['submit']){
$array = unserialize(base64_decode($_POST['articles']));
$array['articles'][] = array(
'tax' => $_POST['tax'],
'price' => $_POST['price'],
'description' => $_POST['description']
);
$postData = base64_encode(serialize($array));
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>My Simple Form</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body style="background-color:#b3ffff">
<div style="padding-left:500px; padding-top:200px">
<form action="" method="post" enctype="multipart/form-data">
Tax: <input type="text" name="tax" placeholder="tax" value=""><br>
Price: <input type="text" name="price" placeholder="price" value=""><br>
Description <input type="text" name="description" placeholder="description" value=""><br>
<input type="hidden" name="articles" value=" <?php echo $postData; ?> ">
<input type="submit" name="submit" value="Submit">
</form>
</div>
</body>
</html>
<?php
echo
'<pre>';
print_r($array);
echo
'</pre>';
//When you need to convert it to a json string then use this:
$jsonString = json_encode($array);
?>
The second way
This way does not use a hidden input field. Instead we are just going to pass the post data to a $_SESSION variable which will store the array in memory on the server side. Just make sure you delete the session variable when you decide to leave the page because it will always be there if you don't. What I mean by that is that if you reload the page at a later time, all the data from the first time you were on the page will still be there.
session_start();
if(isset($_POST['submit']) && $_POST['submit']){
$_SESSION['myData']['articles'][] = array(
'tax' => $_POST['tax'],
'price' => $_POST['price'],
'description' => $_POST['description']
);
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>My Simple Form</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body style="background-color:#b3ffff">
<div style="padding-left:500px; padding-top:200px">
<form action="" method="post" enctype="multipart/form-data">
Tax: <input type="text" name="tax" placeholder="tax" value=""><br>
Price: <input type="text" name="price" placeholder="price" value=""><br>
Description <input type="text" name="description" placeholder="description" value=""><br>
<input type="submit" name="submit" value="Submit">
</form>
</div>
</body>
</html>
<?php
echo
'<pre>';
print_r($_SESSION['myData']);
echo
'</pre>';