echo in view仅打印从控制器传递的数组中的第一个字符

时间:2014-04-29 17:26:46

标签: php codeigniter

controller.php

 function post_action()
    {   
        $checkin=$_POST['checkin'];
        $checkout=$_POST['checkout'];
        $adult=$_POST['adult'];
        $child=$_POST['child'];


       $data['abc']= array("checkin"=>$checkin, "checkout"=>$checkout, "adult"=>$adult, "child"=> $child);
               $this->load->view('template/reservation_template',$data);
    }

视图

  <script type="text/javascript">
 function changeFunc() {

  var checkin = $("#CheckIn").val();
  var checkout = $("#CheckOut").val();
 var adult = $("#adult").val();
  var child = $("#child").val();
$.ajax({
type: "POST",
url: "<?php echo base_url().'index.php/room_booking/post_action' ;?>",
data: {
 'checkin' : checkin,
 'checkout' : checkout,
 'adult' : adult,
 'child' : child,
    },
  success: function(msg) 
    {

        $("#container").html(msg);

    }
 });
 }

 <?php
    $adultsNumber = 5;
    $children = 5;
    ?>
 <div id="container">   // i want to display data from ajax in this div.

<div id="body">

       <div class ="checkForm">
           <form method="post" action="#" id="checkin_room">
        <table>
            <tr>
                <td class="tabledata">

                </td>
                <td class="tabledata">
                  <div class="input-prepend input-append">
            <span class="add-on">Check In</span>
            <input name="CheckIn" type="text" required="required" style="width:185px; cursor:pointer;" id="CheckIn" value="">
            <span class="add-on" style="width:auto; "><img src='<?php echo base_url().'contents/images/ParkReserve.png' ;?>' style="width: 15px; height: 20px;"   ></span>
            </div> 
                </td>
                <td class="tabledata">
                    Adults 
                </td>
                <td class="tabledata">
                    <select name="adults" id="adult">

                        <?php
                        for ($i = 1; $i <= $adultsNumber; $i++) {
                            echo "<option value=" . $i . ">" . $i . "</option>";
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <br/>
            <tr>
                <td class="tabledata">

                </td>
                <td class="tabledata">
                   <div class="clear"></div>
            <div class="input-prepend input-append">
            <span class="add-on">Check Out</span>
            <input name="CheckOut" type="text" style="width:185px; cursor:pointer;" required="required" id="CheckOut" value="">
            <span class="add-on" style="width:auto;"><img src='<?php echo base_url().'contents/images/ParkReserve.png' ;?>' style="width: 15px; height: 20px;" ></span>
            </div>
                </td>
                <td class="tabledata">
                    Children
                </td>
                <td class="tabledata">    
                    <select name="children" required id="child">

                        <?php
                        for ($i = 1; $i <= $children; $i++) {
                            echo "<option value=" . $i . ">" . $i . "</option>";
                        }
                        ?>
                    </select>
                </td>
                <td class="tabledata">
                    <input type ="button" value="Submit"   onclick="javascript:changeFunc();">

                </td>
        </table>
    </form>




    </div>
    </div>

  <?php
    if(!empty($abc))    // this is where i am fetching data from controller to view.
    {

    foreach($abc as $g)
    {
        //var_dump($g);
    echo  $g["checkin"];
    }
    }

    ?>
 </div>

我很惊讶,为什么echo $ g [&#34; checkin&#34;]只给我一个来自输入字段的第一个字符,而var_dump给出输入的全部值。我做错了什么。请帮帮我。

1 个答案:

答案 0 :(得分:0)

首先,如果您通常想要在视图中回显数据,则可以调用模型来获取数据。

现在,您可以将$ _POST值放在一个数组中,如下所示:

$data = array(
"checkin"=>$checkin,
"checkout"=>$checkout,
"adult"=>$adult,
"child"=> $child
);

$this->load->view('template/reservation_template',$data);

一般来说,这些值来自这样的模型,就像在控制器中调用一样:

$data['values'] = $this->Model_name->get_some_data();

在视图中,您可以循环使用以下值:

foreach($values as $value)
{
   echo $value['checkin];
}