Just have simple question. I have PHP page with ajax, that generates some forms. Those forms could be submitted with ajax. the data, that gonna be send to PHP action script, could be either $POST['city0']
or $POST['city5']
or $POST['city20']
and etc..
So how can I have access to this POST data? is that something like :
$city = POST['city[..]'] ;
possible? whats is the right syntax?
Thanx in advance!
Set the input name attribute as name="city[]"
, that way $_POST['city']
will be an array.
Doing it like this you will have to try every single possibility from city0
to cityN
.
I suggest you name your parameters like this: city[X]
where X is the number.
Then you can simply cycle through all the values with a foreach like this:
foreach($_POST['city'] as $key => $city) "city[" . $key . "] = " . $city;
You could use $_POST["city".(string)0];
to get them.
Or loop through them with
for( $i=0; $i != 5; $i++ ){
echo $_POST["city".$i];
}
If you can modify your forms thought, @Danijel's answer is the best, passing the data back as an array.
If you are not concerned with the index of the items in the array you can go with this simple array syntax:
<form name="example" action="" method="POST">
<input type="text" name="city[]">
<input type="text" name="city[]">
<input type="text" name="city[]">
</form>
Or you can give them numbered indexes:
<form name="example" action="" method="POST">
<input type="text" name="city[15]">
<input type="text" name="city[7]">
<input type="text" name="city[203]">
</form>
Or string indexes:
<form name="example" action="" method="POST">
<input type="text" name="city[ny]">
<input type="text" name="city[ca]">
<input type="text" name="city[or]">
</form>
Whatever form you choose you will access by:
$myVar = $_POST['city'][$index];
And, as always, print_r()
is your friend because it will show you the structure of your data if you don't know or can't remember:
print_r($_POST);
Will give you something like:
Array
(
[city] => Array
(
[ny] => one
[ca] => two
[or] => three
)
)
I was really thinking, that there in PHP is it possible to use "bash like" style, similar to this - touch file{1..3} && rm file{1..3}
Finally I gave up and changed checkboxes arrays and form names to more specified. And to give a clue to the receiving PHP script. Before it was:
$counter = 0;
foreach ($states as $state){
echo "<form id=\"form" . $counter . "\">";
// Here we do MySQL request and pulling out cities
// where state=$state to $cities array...
// Then unpack em as a check-boxes:
foreach($cities as $city){
echo "<input type=\"checkbox\" name=\"city" . $counter . "[]\" ...
...
$counter++;
}
and now i've changed it to:
foreach ($states as $state){
echo "<form id=\"form_" . $state . "\">";
// Here we do MySQL request and pulling out cities
// where state=$state to $cities checkbox array...
// Then unpack em as a check-boxes:
foreach($cities as $city){
echo "<input type=\"checkbox\" name=\"city_" . $state . "[]\" ...
...
echo "<input type=hidden name=state value=".$state.">";....
Because $state has no duplicates by default, so all generated elements will have their own unique names
And now I can easily have access to $_POST['city_?']
:
$state = $_POST['state'];
echo $_POST['city_'.$state];
and so on.. And thank you everyone, who have helped me out here