将多个复选框值传递给控制器

时间:2014-01-21 16:02:15

标签: codeigniter

我正在开发codeigniter中的电影票预订系统。用户可以通过选中每个可用座位上的框来选择他的座位。当我尝试将已检查的值存储在会话数据中时,我得到一个错误。下面是我的代码。

查看select_seat

<!DOCTYPE html>
<html>
<head>

<title>Select Seats</title>
<link rel="stylesheet" type="text/css" href="http://localhost/cinema/assets/css/form2.css"/>
</head>
<body>
<form method="post" action="http://localhost/cinema/Movies/select_combo" name="selectshow" accept-charset="utf-8" class="username">
<?php

$prevRowId = null;
$seatColor = null;
$tableRow = false;
//echo $result;
echo "<table width='100%' border='0' cellpadding='0' cellspacing='2'>";
foreach($query as $key=>$result)
{
  $rowId = $result['rowId'];
  $status = $result['status'];
  $columnId = $result['columnId'];
  $typeID=$result['seat_type_id'];
if ($prevRowId != $rowId) {
 if ($rowId != 'A') {
  echo "</tr></table></td>";
  echo "\n</tr>";
}
 $prevRowId = $rowId;
 echo "\n<tr><td align='center'><table border='0' cellpadding='5' cellspacing='2'>      <tr>";
 } else {
$tableRow = false;
}
 if ($status == 0) {
 if($typeID==1)   
    $seatColor = "grey";
 else if($typeID==2)
  $seatColor="yellow";
else  
  $seatColor="white";
 } 
else {
 $seatColor = "red";
}

 echo "\n<td bgcolor='$seatColor' align='center'>";
 echo "$rowId$columnId";
 if ($status == 0)
{
 echo "<input type='checkbox' name='seats[]' value='$rowId$columnId'></checkbox>";
}
echo "</td>";
 if (($rowId == 'A' && $columnId == 6)
   || ($rowId == 'B' && $columnId == 7)
   || ($rowId == 'C' && $columnId == 7)
    || ($rowId == 'D' && $columnId == 7)
) {
// This fragment is for adding a blank cell which represent the "center aisle"
echo "<td> </td>";
}
 }
 echo "</tr></table></td>";
 echo "</tr>";
 echo "</table>";

mysql_close();
?>
<input type="submit" class="button-link" style="float:right; margin-top: 20px; margin-right:0px; margin-bottom: 10px;" value="Submit" />
    </form>
</body>
</html>

模型

 <?php 
 class Selectseat extends CI_Model {
   public function __construct()
   {
      $this->load->database();
   }

   function get() {
      return $this->db->query("SELECT * from seats order by rowId, columnId asc")
             ->result_array();
   }
 }
 ?>

控制器

 function select_combo(){
       foreach($this->input->post('seats[]') as $seat)
            $data['seats']=$seat;
    $this->session->set_userdata($data);

       $this->load->model('combo_model');
    $data['combos'] = $this->combo_model->get_combo();
    $this->load->view('header');
       $this->load->view('select_combo',$data);


   }

我收到此错误

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/Movies.php

1 个答案:

答案 0 :(得分:1)

快速发现:

您不能用作

$this->input->post('seats[]')

相反

$seats = $this->input->post('seats');

现在座位有数组值。

您可以按以下方式更改代码:

function select_combo(){
    $data['seats']=$this->input->post("seats");
    $this->session->set_userdata($data);

    $this->load->model('combo_model');
    $data['combos'] = $this->combo_model->get_combo();
    $this->load->view('header');
    $this->load->view('select_combo',$data);


   }