Using array posting in an HTML form is useful in scenarios where you need to capture multiple instances of similar data, such as a list of items, checkboxes, multiple file uploads, table data, or any other situation where you want to send and process multiple values together.
For this example we create a simple shop manger php app and post table data
<form method="post" action="">
<table class="table table-striped">
<tbody>
<tr>
<th>Name</th>
<th>MRP</th>
<th>Price</th>
<th>Buy</th>
<th>Get</th>
</tr>
<tr>
<input type="hidden" id="Id" name="id[]" value="tomato">
<td><label>Tomato 1Kg</label></td>
<input type="hidden" name="name[]" value="Tomato 1Kg">
<td><input type="text" name="mrp[]" value="99" style="max-width: 80px;"></td>
<td><input type="text" name="price[]" value="30" style="max-width: 80px;"></td>
<td><input type="text" name="buy[]" value="5Kg" style="max-width: 80px;"></td>
<td><input type="text" name="get[]" value="1Kg" style="max-width: 80px;"></td>
</tr>
....
....
....
</tbody>
</table>
<input type="submit" class="btn btn-primary" value="Submit">
<a class="btn btn-success" href="/" role="button">Shop</a>
</form>
Above code display input fields in table layout. To post a table of data from HTML form we use the name attribute identifies the input field. For example name=“mrp[]” means the input field is part of an array of input fields with the name mrp[]. When the user submits the form, the browser will send the values of all the input fields in the get array to the server.
echo (var_dump($_POST)); in a php code will dump the values as below.
array(6) {
["id"]=> array(5) { [0]=> string(6) "tomato" [1]=> string(6) "potato" [2]=> string(8) "eggplant" [3]=> string(4) "okra" [4]=> string(6) "radish" }
["name"]=> array(5) { [0]=> string(10) "Tomato 1Kg" [1]=> string(10) "Potato 1Kg" [2]=> string(12) "Brinjal 250g" [3]=> string(9) "Okra 100g" [4]=> string(17) "White Radish 250g" }
["mrp"]=> array(5) { [0]=> string(2) "99" [1]=> string(2) "50" [2]=> string(3) "120" [3]=> string(2) "28" [4]=> string(2) "55" }
["price"]=> array(5) { [0]=> string(2) "30" [1]=> string(2) "46" [2]=> string(2) "99" [3]=> string(2) "25" [4]=> string(2) "30" }
["buy"]=> array(5) { [0]=> string(3) "5Kg" [1]=> string(3) "5Kg" [2]=> string(3) "5Kg" [3]=> string(4) "200g" [4]=> string(4) "500g" }
["get"]=> array(5) { [0]=> string(3) "1Kg" [1]=> string(3) "1Kg" [2]=> string(3) "1Kg" [3]=> string(3) "50g" [4]=> string(4) "200g" }
}
Full Code ➥
Below is the fully working php script.
In this script the data will be stored in a json file data.json. funtions read_json_file() and save_form_data() are helper functions to read and save data to data.json.
The code then reads the data.json file using read_json_file(). If the request method is POST, the code then iterates through the POST data and creates an array of items. Each item in the array contains the following information: id, name, mrp, price, buy, get
The code then adds the array of items to the mainEntity key of the data array. The code then saves the data array to the data.json file using save_form_data().
Finally, the code renders an HTML form. The form allows the user to enter the values for each item.
File: manage.php
<?php
// Function to read JSON file and return as an associative array
function read_json_file($filename)
{
$json_data = file_get_contents($filename);
$data = json_decode($json_data, true);
return $data;
}
function save_form_data($filename, $data)
{
$json_data = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents($filename, $json_data);
}
// Read JSON file
$data = read_json_file('data.json');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$items = array();
$arr_id = $_POST["id"];
$arr_name = $_POST["name"];
$arr_mrp = $_POST["mrp"];
$arr_price = $_POST["price"];
$arr_buy = $_POST["buy"];
$arr_get = $_POST["get"];
for ($i = 0; $i < sizeof($_POST['id']); $i++) {
$row = array(
"id" => $arr_id[$i],
"name" => $arr_name[$i],
"mrp" => $arr_mrp[$i],
"price" => $arr_price[$i],
"buy" => $arr_buy[$i],
"get" => $arr_get[$i]
);
array_push($items, $row);
}
$data = array(
"mainEntity" => $items
);
echo (var_dump($_POST));
save_form_data('data.json', $data);
}
if (true):
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ZamZam Fresh Manager</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col text-center">
<h1>Shop Manager</h1>
</div>
</div>
<div class="row">
<div class="col">
<form method="post" action="">
<table class="table table-striped">
<tr>
<th>Name</th>
<th>MRP</th>
<th>Price</th>
<th>Buy</th>
<th>Get</th>
</tr>
<?php foreach ($data["mainEntity"] as $item): ?>
<tr>
<input type="hidden" id="Id" name="id[]" value="<?= $item["id"]; ?>">
<td><label>
<?= $item["name"]; ?>
</label></td>
<input type="hidden" name="name[]" value="<?= $item["name"]; ?>">
<td><input type="text" name="mrp[]" value="<?= $item["mrp"]; ?>" style="max-width: 80px;"></td>
<td><input type="text" name="price[]" value="<?= $item["price"]; ?>" style="max-width: 80px;"></td>
<td><input type="text" name="buy[]" value="<?= $item["buy"]; ?>" style="max-width: 80px;"></td>
<td><input type="text" name="get[]" value="<?= $item["get"]; ?>" style="max-width: 80px;"></td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" class="btn btn-primary" value="Submit">
<a class="btn btn-success" href="/" role="button">Shop</a>
</form>
</div>
</div>
</div>
</body>
</html>
<?php endif; ?>
File: data.sjon
{
"mainEntity": [
{
"id": "tomato",
"name": "Tomato 1Kg",
"mrp": "99",
"price": "30",
"buy": "5Kg",
"get": "1Kg"
},
{
"id": "potato",
"name": "Potato 1Kg",
"mrp": "50",
"price": "46",
"buy": "5Kg",
"get": "1Kg"
},
{
"id": "eggplant",
"name": "Brinjal 250g",
"mrp": "120",
"price": "99",
"buy": "5Kg",
"get": "1Kg"
},
{
"id": "okra",
"name": "Okra 100g",
"mrp": "28",
"price": "25",
"buy": "200g",
"get": "50g"
},
{
"id": "radish",
"name": "White Radish 250g",
"mrp": "55",
"price": "30",
"buy": "500g",
"get": "200g"
}
]
}