How to send output with multiple values from extjs

2019-03-04 11:39发布

I am working in extjs+yii. My server side is in yii framework and client side is in extjs. Now I want to pass extjs's submit buttons output to yii action. I am creating multiple choice question paper of 20 questions in extjs whose actual questions will come from server side action which is written in Yii framewok. Up to this, it is working correctly.

Now after solving all questions by marking its respective radio buttons as an answer, on the click of submit button I want to send these 20 questions userId, questionId and selected radio buttons option to yii controller action. I had written submit button action as:

     check:function()
            {
        console.log("Inside check function.");
        //creating objects in javascript
          var obj=new Object();
          for(var i=0;i<=5;i++)
          {
                var inputs = document.getElementsByName(i); 
                var radio = "";  
                for (var j = 0; j < inputs.length; j++) {
                    if (inputs[j].checked) {
                        name = inputs[j].name;
                        value  = inputs[j].value;
                        //obj[i].name1=name;
                        obj[i]={'questionId':name,'option':value};
                        console.log("questionId="+name +" value="+ value);
                        console.log("object name="+ obj[i].questionNo+" Object value="+obj[i].option);
                    }
                }
          }
    }
});

So I am getting questionId and optionValue of all questions on submit button click. Now I want to send all the questionid and optionValue data to yii action. So how to send it to extjs action?

标签: Extjs yii output
1条回答
Ridiculous、
2楼-- · 2019-03-04 12:13

You should post your data to an action in a controller with AJAX for example : site/savequestions

   Ext.Ajax.request({
        url:"site/savequestions",
        method: "POST",
        params: {'qid': name, 'aid':value},
        success: function(){
            console.log("ok");
        },
        failure: function(response, opts){
            console.log("failed");
        },
        headers: { 'Content-Type': 'application/json' }
    });

and then in the controller SiteController you would have

public function actionSavequestion()
{
    $questionId = Yii::app()->request->getParam('qid');
    $anserId = Yii::app()->request->getParam('aid');
    //... do stuff here

    echo json_encode(array('success' => true));
    exit()
}
查看更多
登录 后发表回答