I think your JS should form the input field with name similar to:
name="data[GalStore][gal_sublocation_id][]"
سؤال
I have a form to add addresses in which one of the field is if I select the city, it will display the locations of the city. my add.ctp
is :
<?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>
But when I var_dump($this->data)
it is not showing all the fields of the form ,
it outputs like below :
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)
The 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);
}
}
Why it is not showing the sublocation ids ? Where I amdoing wrong ? Please help.
المحلول
I think your JS should form the input field with name similar to:
name="data[GalStore][gal_sublocation_id][]"