Automatic store POST vars (array) from form with foreach.

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
genderbee
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Feb 13, 2020 12:27 pm

Thu Feb 13, 2020 12:39 pm

Hello,
how can I store all fields from form (KEY => VALUE) via POST method with for example foreach? Instead of manual writing. I send data via API then.

Example (bad).

Code: Select all

$polozky = array(
"prepravce",
"eid",
"vs",
"service_type",
"cod_price",
"price",
"cod_currency",
"rec_phone",
"rec_name",
"rec_street",
"rec_city",
"rec_country",
"rec_email",
"rec_zip",
"rec_evening"
);

# HERE IS WHAT I WANT, BUT IT IS NOT WORKING

$data=array(
foreach($polozky as $polozka)
	{
		$polozka => POST['polozka'];
	}
);

?>

<form method="post">

<?php

foreach($polozky as $polozka)
{
echo'
<p>'.$polozka.'<input type="text" name="'.$polozka.'" value="'.$_POST[$polozka].'" placeholder="'.$polozka.'"></p>
';
}

echo'
<p>
<input type="submit" value="Submit">
</p>
';

?>
Is my question clear?

Thanks.
chorn
php-forum GURU
php-forum GURU
Posts: 653
Joined: Fri Apr 01, 2016 2:18 am

Thu Feb 13, 2020 11:02 pm

store... where? the keys/values are already "stored" in the _POST variable.
Post Reply