Thursday, April 12, 2012

jquery ajax cakephp: change event for input text box displays all fields except city dropdown box

When a user enters an email, the onchange event for the input text box fires and displays all the other fields except the city dropdown box. I noticed that $("#MerryParentCityId").val(result_array[5]); is having the value 81 but the respective city name that user is not getting displayed in MerryParentCityId dropdown box. The value for the state dropdown box is displaying fine. Can someone tell me on where i'm going wrong?



I have a feeling the problem is in the html form's MerryParentCityId dropdown box. But I have no idea on what changes to make there.



thank you.



add.ctp (partial form where city and state dropdown box is located)



echo $this->Form->input('MerryParent.email');
echo $this->Form->input('MerryParent.name', array('label'=>'Parent/Guardian Name'));
echo $this->Form->input('MerryParent.landline');
echo $this->Form->input('MerryParent.mobile');
echo $this->Form->input('MerryParent.address');
echo $this->Form->input('MerryParent.state_id', array('empty'=>'Choose one','options'=>$states));
echo $this->Form->input('MerryParent.city_id');
//echo $this->Form->input('MerryParent.city_id', array('empty'=>'Choose one','options'=>$cities));
echo $this->Form->input('MerryParent.postal_code');


jquery code



<script type="text/javascript">
$(document).ready(function(){


$("#MerryParentEmail").change(function(){
//txt=$("#MerryParentEmail").val();
email_id=$("#MerryParentEmail").serialize();
//alert(txt);
$.post("../students/get_parent_info",email_id,function(result_str){
result_array=result_str.split('<br>');
$("#MerryParentName").val(result_array[0]);
$("#MerryParentLandline").val(result_array[1]);
$("#MerryParentMobile").val(result_array[2]);
$("#MerryParentAddress").val(result_array[3]);
$("#MerryParentStateId").val(result_array[4]);
$("#MerryParentCityId").val(result_array[5]);
$("#MerryParentPostalCode").val(result_array[6]);
});
});

$("#MerryParentStateId").change(function(){
state=$(this).val();
txt_str="state_id="+state;
$.get("../students/getcities",txt_str,function(result){
$("#MerryParentCityId").html(result).show();
});
});



});

</script>


students_controller.php



function get_parent_info(){
//$this->layout=false;
if (!empty($this->data)){

$merryparent_info=$this->Student->MerryParent->getMerryParents($this->data['MerryParent']['email']);

echo $merryparent_info['MerryParent']['name'].'<br>';
echo $merryparent_info['MerryParent']['landline'].'<br>';
echo $merryparent_info['MerryParent']['mobile'].'<br>';
echo $merryparent_info['MerryParent']['address'].'<br>';
echo $merryparent_info['MerryParent']['state_id'].'<br>';
echo $merryparent_info['MerryParent']['city_id'].'<br>';
echo $merryparent_info['MerryParent']['postal_code'].'<br>';
}
}

function getcities(){
$this->data['MerryParent']['state_id']=$_GET['state_id'];
if (!empty($this->data['MerryParent']['state_id'])){
$cities = $this->Student->MerryParent->City->getCities($this->data['MerryParent']['state_id']);
//print_r($cities);
foreach ($cities as $k=>$v){
echo '<option value="'.$k.'">'.$v.'</option>';
}

/* foreach($cities as $optionValue){
echo '<option>' . $optionValue . '</option>';
}*/
}else{
$this->Session->setFlash('You didn\'t select a state!');
}

}

function add(){
//saves a new students information to db
$states=$this->Student->MerryParent->State->getStates();
$this->set('states',$states);

//$cities=array();
}




No comments:

Post a Comment