How to Get a Form Input Array into a PHP Array

How to get a form input array into a PHP array

They are already in arrays: $name is an array, as is $email

So all you need to do is add a bit of processing to attack both arrays:

$name = $_POST['name'];
$email = $_POST['account'];

foreach( $name as $key => $n ) {
print "The name is " . $n . " and email is " . $email[$key] . ", thank you\n";
}

To handle more inputs, just extend the pattern:

$name = $_POST['name'];
$email = $_POST['account'];
$location = $_POST['location'];

foreach( $name as $key => $n ) {
print "The name is " . $n . ", email is " . $email[$key] .
", and location is " . $location[$key] . ". Thank you\n";
}

HTML/PHP - Form - Input as array

Simply add [] to those names like

 <input type="text" class="form-control" placeholder="Titel" name="levels[level][]">
<input type="text" class="form-control" placeholder="Titel" name="levels[build_time][]">

Take that template and then you can add those even using a loop.

Then you can add those dynamically as much as you want, without having to provide an index. PHP will pick them up just like your expected scenario example.

Edit

Sorry I had braces in the wrong place, which would make every new value as a new array element. Use the updated code now and this will give you the following array structure

levels > level (Array)
levels > build_time (Array)

Same index on both sub arrays will give you your pair. For example

echo $levels["level"][5];
echo $levels["build_time"][5];

How to get form input array into PHP array but not empty field

Something like this should work:

foreach ($_POST[Product] as $key => $value):
if (empty($value)):
unset($_POST[Product][$key]);
endif;
endforeach;

foreach ($_POST[Quantity] as $key => $value):
if (empty($value)):
unset($_POST[Quantity][$key]);
endif;
endforeach;

Put form data into a PHP array

Change the name of your inputs like this:

<form action = "put the data to a single PHP array" method="post">
<input name="a[]" type="text" />
<input name="a[]" type="text" />
<input name="a[]" type="text" />
<input name="a[]" type="text" />
<input type="button"/>
</form>

You can then retrieve the array as follows:

$values = $_POST['a'];

How to get 2 value form input array into PHP array

Assuming that you don't have 2 entries having the same opt_id and lang_id then you can use a single key instead of 2:

HTML:

<input type="text" name="input[<?php echo "{$opt_id}_{$lang_id}"; ?>]" />

PHP:

foreach ($_POST['input'] as $optIdAndLangId => $value) {
list($opt_id, $lang_id) = explode('_', $optIdAndLangId);
}

PHP creating an array from form input fields

If you want to get the sum of an array you dont need to loop you can use array_sum

Example

<?php
if (isset($_POST['numbers'])) {
echo array_sum($_POST['numbers']);
}

?>

<form method="POST">
<input type="number" name="numbers[]"/>
<input type="number" name="numbers[]"/>
<input type="number" name="numbers[]"/>
<input type="number" name="numbers[]"/>
<input type="submit" value="add"/>
</form>

How to send a PHP array via HTML form (using input tag)

The easiest way to do that is to convert your categories array to JSON and back.

echo '<input type="hidden" name="category" value="' . json_encode($category) . '" />';

... and when you send your form, parse the JSON back to the array:

$category = json_decode($_GET["category"]);


Related Topics



Leave a reply



Submit