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 only echo
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(";", ";
", $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.
Changes
1) Change name='hotelul[$i][]'
to name='hotelul[]'
2) All input
names are hotelul
. Change to some other name to avoid ambiguity.
Updated Code:
<form action='chior.php' method='POST'>
<?php
$i = 0;
foreach($counter as $obj => $nr_rez) {
$nume_hotel = $hoteluri[$obj];
$localitate = $localitati[$obj];
$total_rez += $nr_rez;
$cprest = substr($cprest, 3, 10);
$parametri = "cp=$cprest&dstart=$data_start_af&dstop=$data_stop_af";
$email = str_replace(";", ";
", $emailuri[$obj]);
echo "<tr class='mainRow'>
<td> $i </td>
<td><input type='text' name='hotelul[]' 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_email[]' value='". $email ."'/></td>
<td><input type='checkbox' id='$i' name='hotelul_chkbox[]'/></td>
</tr>";
$i++;
}
?>
<input type='submit'/>
</form>
chior.php
<?php
$checkedHotels = sizeof($_POST['hotelul_chkbox']);
for($i = 0 ; $i < $checkedHotels; $i++){
$checked_hotel_email = $_POST['hotelul_email'][$i];
//Write your mail function here to send mail to all checked hotels using `$checked_hotel_email`.
}?>
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:
<input type="text" name="email[]" />
<input type="text" name="email[]" />
<input type="text" name="email[]" />