Cakephp没有显示所有表单数据

时间:2014-01-27 06:15:00

标签: php forms cakephp post cakephp-1.3

我有一个表单添加地址,其中一个字段是如果我选择城市,它将显示城市的位置。我的add.ctp是:

<?php ?>
<div id="body-content">
    <h1>Add Store Locations</h1>
    <?php echo $form->create("GalStore",array("controller"=>"gal_stores","action"=>"add")); ?>
    <?php echo $form->hidden("key",array("value"=>$key)); ?>
    <table style='line-height:25px;' cellspacing="10px">
        <tr>
            <td>Select Provider :</td>
            <td>
                <?php 
                $attributes = array("empty"=>false);
                echo $form->select("GalStore.gal_provider_id",$gal_providers,null,$attributes); 
                ?>
            </td>
        </tr>
        <tr>
            <td>Address</td>
            <td><?php echo $form->textarea("address",array("label"=>"")); ?></td>
        </tr>

        <tr>
            <td>About</td>
            <td><?php echo $form->textarea("GalStore.about",array("label"=>"")); ?></td>
        </tr>



        <tr>
            <td>City</td>
            <td><?php 
            $city_attributes = array("empty" => "-- Select City ---","id" => "locations_list");
            echo $form->select("GalStore.gal_location_id", $gal_locations,null,$city_attributes); ?>

            </td>
        </tr>
        <tr>
            <td>Location</td>
            <td id="load_sublocations">

            </td>
        </tr>

        <tr>
            <td>Contact Number</td>
            <td><?php echo $form->input("contact_num",array("label"=>"")); ?></td>
        </tr>
        <tr>
            <td>Other Info</td>
            <td><?php echo $form->input("other_info",array("label"=>"")); ?></td>
        </tr>
        <tr>
            <td></td>
            <td><?php echo $form->end("Add"); ?></td>
        </tr>
    </table>    

    <h3>Existing Store locations</h3>
    <div>
        <?php
        foreach($gal_stores as $store){
            echo "<div style='padding-bottom:10px'>";;
            echo $store["GalStore"]["address"]." - ".$store["GalStore"]["city"]." - ".$store["GalStore"]["contact_num"];
            echo "</div>";
        }
        ?>
    </div>
</div>



<script>
$('#locations_list').change(function(){

    $('#load_sublocations').empty();
    var gal_location_id = $('#locations_list').val();
    var url = "<?php echo $html->url(array("controller" => "gal_sublocations", "action" => "api_get_sublocations_by_location")); ?>";
    var data = "gal_location_id="+gal_location_id;
    var html = '';


    $.ajax({
        type: "GET",
        url: url,
        data: data,
        dataType: "json",
        error: function(msg){
            alert('Oops ! Make a halt while we fixing the issue.');
        },
        success: function(msg){
            jQuery.each(msg.gal_sublocations, function(index, gal_sublocation) {
                html += '<input type="checkbox" name="GalStore.gal_sublocation_id[]" value = "'+gal_sublocation.GalSublocation.id+'" />'+gal_sublocation.GalSublocation.name;
                $('#load_sublocations').html(html);
            });  

        }            
    });

});

</script>

但是当我var_dump($this->data)时,它没有显示表单的所有字段, 输出如下:

array (size=1)
  'GalStore' => 
    array (size=7)
      'key' => string 'asdg213yt' (length=9)
      'gal_provider_id' => string '237' (length=3)
      'address' => string 'sdg sdf' (length=7)
      'about' => string 's dfsdf' (length=7)
      'gal_location_id' => string '16' (length=2)
      'contact_num' => string '444' (length=3)
      'other_info' => string '345' (length=3)

Sublocation API:

function api_get_sublocations_by_location(){
        $this->autoRender = false;
        Configure::write("debug",0);


        $error = false;
        $resp_type = (!isset($_GET["resp_type"]) || $_GET["resp_type"] == "") ? "json" : $_GET["resp_type"];

        $resp_arr = array();

        if(!isset($_GET["gal_location_id"]) || $_GET["gal_location_id"] == ""){
            $resp_arr["error"] = true;
            $resp_arr["msg"] = "Invalid Location requested";
        }else{
            $gal_sublocations = $this->GalSublocation->getList($_GET["gal_location_id"],50,false);

            //in case of any DB error
            if(!$gal_sublocations){
                $resp["error"] = true;
                $resp["msg"] = "Oops .. seems like something went wrong";
            }else{
                $resp["error"] = false;
                $resp["gal_sublocations"] = $gal_sublocations;
            }            
            return json_encode($resp);

        }
    }

为什么它没有显示子位置ID?哪里出错了?请帮忙。

1 个答案:

答案 0 :(得分:1)

我认为你的JS应该形成一个输入字段,其名称类似于:

name="data[GalStore][gal_sublocation_id][]"