How to Pass Form Input Array to a PHP Page?

Problem:

In your HTML form there may have multiple form fields for a single title. For ex. address (see the following picture), it has multiple input boxes. You declared the same name as address[] for all the boxes. Now, when someone enters the address fields and submit the form, you want to retrieve the full address in the PHP page.

Form input arraySolution:

Let’s see the process step by step-

Step 1: The form code

Let’s start from the HTML form. Here is the code for the above HTML form-

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>How to Pass Form Input Array to a PHP Page</title>
<style>
div.left{
    float:left;
    display:block;
    width:10%;
    text-align:right;
    padding-right:5px;
    height:75px;
}
input.txtbox{
    width:240px;
}
div.right{
    float:left;
    display:block;
    width:85%;
    height:25px;
}
input.btn{
    margin-left:10.25%;
}
</style>
</head>

<body>
<form action="submit.php" method="post">
    <div class="left">Address :</div>
    <div class="right"><input name="address[]" type="text" class="txtbox"></div>
    <div class="right"><input name="address[]" type="text" class="txtbox"></div>
    <div class="right"><input name="address[]" type="text" class="txtbox"></div>
    <input name="Submit" type="submit" value="Submit" class="btn">
</form>
</body>
</html>

Please note that we’ve declared all the address fields with the same array name – address[].

Step 2: The PHP code

Now, let’s see how we read the address[] array variables in the PHP code next page after someone submit the form entering his address-

<?php
$address = $_POST['address'];
foreach($address as $value)
    echo $value . "<br />";
?>

[wpdm_file id=105]

Output:
1589 Tyler Avenue
Miami, FL 33169
USA