php - How to pass arrays and nested arrays from Controller to Javascript in View in Yii framework -
i have array in controller , nested arrays inside result of query database. want pass data javascript ajax , update content on site when check checkbox. pls show me example or give me advice how it.
controller
public function actiontestresponse(){ $id = $_post['id']; $checkval = $_post['checkval']; $this->layout = 'newhome'; $criteria=new cdbcriteria(); if($id == 'checkfemale'){ $criteria->compare('sex', 'female'); } else if ($id == 'checkfemale'){ $criteria->compare('sex', 'male'); } $items = user::model()->findall($criteria); //if($items==null||empty($items)){$items='null';} //$response = array('id'=>$id, 'checkval'=>$checkval, array('data' => $items)); $response = array('id'=>$id, 'checkval'=>$checkval); //$data = array('data'=>$items); //$result = array_merge($response, $data); //var_dump($result); //die; echo json_encode($response); }
view
<input type="checkbox" id="checkfemale" class="checktest">female <script type="text/javascript"> $(function() { $(".checktest").click(function() { var id = $(this).attr("id"); if($(this).is(':checked')) { var checkval = 1; } else { var checkval = 0; } var string = 'id='+ id + '&checkval='+ checkval; $.ajax({ type: "post", url: "testresponse", data: string, datatype: "json", success: function(response){ if(response.checkval == 1) { $("#"+response.id).prop('checked', true); } else { $("#"+response.id).removeattr('checked'); } } }); return false; }); }); </script>
example of array
array(3) { ["id"]=> checkfemale ["checkval"]=> 0 ["data"]=> array(2) { [0]=> object(user)#60 (11) { ["_new":"cactiverecord":private]=> bool(false) ["_attributes":"cactiverecord":private]=> array(6) { ["id"]=> int(1) ["user_id"]=> int(46) ["sex"]=> string(6) "female" ["certificate"]=> int(1) ["date_added"]=> string(10) "2015-02-02" ["date_of_birth"]=> string(10) "2015-01-01" } ["_related":"cactiverecord":private]=> array(0) { } ["_c":"cactiverecord":private]=> null ["_pk":"cactiverecord":private]=> int(1) ["_alias":"cactiverecord":private]=> string(1) "t" ["_errors":"cmodel":private]=> array(0) { } ["_validators":"cmodel":private]=> null ["_scenario":"cmodel":private]=> string(6) "update" ["_e":"ccomponent":private]=> null ["_m":"ccomponent":private]=> null } [1]=> object(user)#61 (11) { ["_new":"cactiverecord":private]=> bool(false) ["_attributes":"cactiverecord":private]=> array(6) { ["id"]=> int(3) ["user_id"]=> int(53) ["sex"]=> string(4) "male" ["certificate"]=> int(0) ["date_added"]=> string(10) "2015-02-02" ["date_of_birth"]=> string(10) "2013-06-08" } ["_related":"cactiverecord":private]=> array(0) { } ["_c":"cactiverecord":private]=> null ["_pk":"cactiverecord":private]=> int(3) ["_alias":"cactiverecord":private]=> string(1) "t" ["_errors":"cmodel":private]=> array(0) { } ["_validators":"cmodel":private]=> null ["_scenario":"cmodel":private]=> string(6) "update" ["_e":"ccomponent":private]=> null ["_m":"ccomponent":private]=> null } } }
i solved it. here solution. thank comments.
controller
public function actiontestresponse(){ $id = $_post['id']; $checkval = $_post['checkval']; $this->layout = 'newhome'; $criteria=new cdbcriteria(); if($id == 'checkfemale'){ $criteria->compare('sex', 'female'); } else if ($id == 'checkfemale'){ $criteria->compare('sex', 'male'); } $items = user::model()->findall($criteria); $list = array(); foreach ( $items $item ) { $list[] = array( 'id' => $item->id, 'sex' => $item->sex, ); } $result = array('id'=>$id, 'checkval'=>$checkval, 'data' => $list); echo json_encode($result); }
view
$.ajax({ type: "post", url: "testresponse", data: string, datatype: "json", success: function(response){ $.each(response, function (item, value) { if(item=="checkval"){ if(value == 1){ $("#"+response.id).prop('checked', true); } else { $("#"+response.id).removeattr('checked'); } } else if(item=="data"){ $.each(value, function (i, v) { $.each(v, function (it, va) { console.log(it); }); }); } }); } });
Comments
Post a Comment