douqiao5440 2014-10-02 21:41
浏览 104

如何在Yii中使用JSON

I want to send/get a variable to/from controller action. My codes:

view file

....
<button id="radiyo">radio</button>
<script>
$("#radiyo").on("click", function(){
    var $radio = $('input[type=radio][name=siniflerin-siyahisi]:checked').attr('id');
    $.ajax({
        type: 'POST',
        url: '<?=Yii::app()->baseUrl;?>/ideyalar/sech/radio',
        async: false,
        cache: false,
        data: {radio: $radio},
//        datatype: "html",
        success:function(){

            alert($radio);
        }
    });
    $.ajax({
        type: 'GET',
        url: '<?=Yii::app()->baseUrl;?>/ideyalar/sech/radio',
        async: false,
        cache: false,
        datatype: "json",
        data: {change: $sql},
        success: function(data) {
            alert(data.change);
        }
    });
});
</script>
....

controller action

public function actionSech ($radio)
{
    $sql = Yii::app()->db->createCommand()
        ->select ('m.maraq')
        ->from ('maraq m')
        ->where ('m.idsinif=:ids', [':ids'=>$radio])
        ->queryAll();
    $gonderilen = CJSON::encode(['change'=>$sql]);
}

I read articles from Yii offical site and other forums. But I couldn't understand how can I do it.

Please tell me, how can I send $sql variable to my view file?

Thanks.

  • 写回答

1条回答 默认 最新

  • drzfz9995 2014-10-20 02:15
    关注

    I'm not pretty sure what you want. But, I want to pointing out some snippet.

    In view file

    <?php
        Yii::app()->clientScript->registerScript("header-info","
            var baseUrl = '".Yii::app()->baseUrl;."';  
        ",CClientScript::POS_HEAD);
    ?>
    
    <button id="radiyo">radio</button>
    <script>
        $("#radiyo").on("click", function(){
        var radioValue = $('input[type=radio][name=siniflerin-siyahisi]:checked').attr('id');
        $.ajax({
            url: baseUrl +'/ideyalar/sech',
            dataType:'json',
            type:'POST',        
            data:{radioValue:radioValue},
            async:false
            }).done(function(data){
             if(data['status'] == 'OK'){
                 alert(data['returnValue']);
             }else if(data['status'] == 'ERROR'){
                 alert("HERE WE GO ERROR");
             }
         });
       });
    </script>
    

    Your controller action;

    public function actionSech()
    {
       //In my point, I never call db layer in controller. Controller should be routing purpose
    
       If(Yii::app()->request->isAjaxRequest){
          $radioValue = isset($_REQUEST['radioValue']) ? $_REQUEST['radioValue'] : "";
          $returnObj = array();
          if($radioValue !=""){
             $query = "SELECT `maraq` FROM maraq WHERE idsinif='$radionValue'";
             $result = Yii::app()->db->createCommand($query)->queryScalar();
             if($result != "" || $result != null){  //ensure result is correct or not
                 $returnObj['returnValue'] = $result;
                 $returnObj['status'] = 'OK'; 
             }else{
                 $returnObj['status'] = 'ERROR'; 
             } 
          }else{   //if radiovalue is blank string
             $returnObj['status'] = 'ERROR';
          }
          echo json_encode($returnObj);
       }
    }
    

    Hope this help! Btw, JavaScript variable can't not initialize with $. Just only var yourVar="";

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line