php - Form submission returns 'array'

156

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 onlyechos 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.

300

Answer

Solution:

Changes

1) Changename='hotelul[$i][]' toname='hotelul[]'

2) Allinput names arehotelul. 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(";", ";\n", $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`.

}?>
803

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[];

Thename[] 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[]" />

People are also looking for solutions to the problem: php - OAuth, OpenID or other solutions?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.