php - Form submission returns 'array'
I'm trying to send through POST the name of some hotels, the code, and the e-mail address to a new page that will send an email to the checked hotels. So far, though, what I'm really trying to do it send the data to a new php which onlyecho
s them. What I've worked on so far is:
<form action='chior.php' method='post'>
<?php
$i = 0;
foreach($counter as $obj => $nr_rez) {
$nume_hotel = $hoteluri[$obj];
$localitate = $localitati[$obj]; //all this arranges the data from a sql query
$email = $emailuri[$obj];
$total_rez += $nr_rez;
$cprest = substr($cprest, 3, 10);
$parametri = "cp=$cprest&dstart=$data_start_af&dstop=$data_stop_af";
$email = str_replace(";", ";\n", $email);
echo "<tr class='mainRow'> <td> $i </td>
<td><input type='text' name='hotelul[$i][]' value='".$cprest."' readonly/> </td>
<td><a href='link.php?$parametri' target='_blank'>$nume_hotel</a></td>
<td> $localitate </td>
<td> $nr_rez </td>
<td><input type='text' name='hotelul[$i][]' value='". $email ."'/></td>
<td><input type='checkbox' id='$i' name='hotelul[$i][]'/></td>
</tr>";
$i++;
}
?>
<input type='submit'/> </form>
There's a bit of the page I haven't posted, for brevity's sake (various tags and css elements to make the page look nicely), but it works on my end. The only problem is that the page I'm sent to after clicking submit - chior.php, which looks like this<?php echo $_POST['hotelul'];?>
, returns 'Array'. I've also tried<?php echo implode('/', $_POST['hotelul']);?>
,<?php echo implode('-', implode('/', $_POST['hotelul']));?>
,<?php echo $_POST['hotelul[][]']
, which was pretty much all I could think of and it still did not work. Does anyone have any ideea why that is and how I could fix this? Thanks.
Answer
Solution:
Changes
1) Change
name='hotelul[$i][]'
toname='hotelul[]'
2) All
input
names arehotelul
. Change to some other name to avoid ambiguity.Updated Code:
chior.php
Answer
Solution:
You are using:
<td><input type='checkbox' id='$i' name='hotelul[$i][]'/></td>
which means that hotelul is containing a list (array). If you want to save a single value, remove
[]
;The
name[]
sintaxis is used when you want to$_POST['name']
to be a list. Like this: