How do I handle multi-dimensional arrays sent via $_POST?
1103 Aug 2024
Handling multi-dimensional arrays with $_POST
involves accessing nested arrays sent from an HTML form. Here’s an example:
<form method="post" action="process.php">
<label for="items">Items:</label>
<input type="text" name="items[0][name]" placeholder="Item 1 name">
<input type="text" name="items[0][quantity]" placeholder="Item 1 quantity">
<input type="text" name="items[1][name]" placeholder="Item 2 name">
<input type="text" name="items[1][quantity]" placeholder="Item 2 quantity">
<input type="submit" value="Submit">
</form>
In process.php
, access the data:
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$items = $_POST["items"];
foreach ($items as $item) {
$name = $item["name"];
$quantity = $item["quantity"];
echo "Item Name: " . htmlspecialchars($name) . ", Quantity: " . htmlspecialchars($quantity) . "<br>";
}
}
?>
Make sure to use htmlspecialchars
to escape output for security.
0 likes
Top related questions
Related queries
Latest questions
Maine instagram se paise Kamya hai
08 Nov 2024 1
1000 view pe kitne paise milenge Naya Google pe. Abhi tak yha par itni audience nhi hai lekin jald hi hogi
08 Nov 2024 2
वॉल्यूम बढ़ने और वाइब्रेशन स्विच चालू होने पर भी आपका फ़ोन अचानक से चुप क्यों हो जाता है?
08 Nov 2024 2
Online earning kese kare
07 Nov 2024 7
Grow Your Channel 10x FASTER with PROVEN Strategy
07 Nov 2024 11
1 like kar do
07 Nov 2024 5
Good night
07 Nov 2024 2
छोटे YouTuber 2 Setting ऑन कर दो Youtube खुद करेगा Boost और Monetize | Subscriber kaise badhaye 2025
07 Nov 2024 16
यूट्यूब शॉर्ट्स से पैसे कैसे कमाए ?
07 Nov 2024 15
इंस्टाग्राम से पैसा कैसे कमाए ?
07 Nov 2024 12