erol_smsr
erol_smsr

Reputation: 1496

Handle form data with array PHP

I have an HTML form that looks like this:

<form method="POST" action="ideal_test.php">
<input type="text" name="member[email]" placeholder="email" /><br />
<input type="text" name="member[country_code]" placeholder="country code" /><br />
<input type="text" name="member[first_name]" placeholder="first name" /><br />
<input type="text" name="member[last_name]" placeholder="last name" /><br />
<input type="text" name="member[street]" placeholder="street" /><br />
<input type="text" name="member[house_nr]" placeholder="house number" /><br />
<input type="text" name="member[houseNrExtension]" placeholder="house number addition" /><br />
<input type="text" name="member[postcode]" placeholder="postcode" /><br />
<input type="text" name="member[city]" placeholder="city" /><br />
<input type="text" name="member[phone]" placeholder="phone number" /><br />
<input type="text" name="member[birthday]" placeholder="birthday" /><br />
<input type="text" name="member[gender]" placeholder="gender" /><br />
<input type="text" name="member[ideal_bank]" placeholder="iDeal bank" /><br />
<input type="hidden" name="payment" value="ideal" />

<input type="submit" value="Test iDeal API call" />

The reason the form has been made this way is because I use an API that has to receive the data in this format. When I change the form action to the API URL, it works, however, sometimes I have to handle the form data myself with my own PHP and I don't know how I should process this form with PHP.

This is what I've tried:

$data = $_POST[ 'member' ];

foreach( $data AS $row ) {
    echo $row . '<br />';
}

This shows all the data nicely separated by the <br />. However, I need to access each input separately, so I've tried this to achieve that:

foreach( $data AS $row ) {
    echo $row['email'] . '<br />';
}

When I do this, it displays the first letter of each input, also separated by <br />. How can I loop through the member[] array and get every input separately?

Upvotes: 0

Views: 526

Answers (1)

Alok Patel
Alok Patel

Reputation: 8022

$data is an array.
So if you want to access each input separately You don't need to do it in foreach loop.

You simply print it as normal array variable. Like this:

<?php
     echo $data['email'];
     echo $data['first_name'];
     // Like this.
?>

Upvotes: 2

Related Questions