表单提交返回'array'



我试图通过POST将一些酒店的名称、代码和电子邮件地址发送到一个新页面,该页面将向所检查的酒店发送电子邮件。到目前为止,我真正想做的是将数据发送到一个新的php,它只有echo。到目前为止我所做的是:

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

为了简洁起见,我有一些页面没有发布(各种标签和css元素使页面看起来很漂亮),但它在我这边工作。唯一的问题是,我被发送到点击提交后的页面- chior.php,这看起来像这个<?php echo $_POST['hotelul'];?>,返回'数组'。我也试过<?php echo implode('/', $_POST['hotelul']);?>, <?php echo implode('-', implode('/', $_POST['hotelul']));?>, <?php echo $_POST['hotelul[][]'],这几乎是我能想到的所有,它仍然不起作用。有人知道这是为什么吗?我该如何解决这个问题?谢谢。

变化

1)name='hotelul[$i][]'更改为name='hotelul[]'

2)所有input的名字都是hotelul。更改为其他名称以避免歧义。

更新代码:

<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`.
}?>

您正在使用:

<td><input type='checkbox' id='$i' name='hotelul[$i][]'/></td>

表示hotelul包含一个列表(数组)。如果您想保存单个值,请删除[];

当您希望$_POST['name']是一个列表时,使用name[] sintaxis。这样的:

<input type="text" name="email[]" />
<input type="text" name="email[]" />
<input type="text" name="email[]" />

最新更新