简体   繁体   中英

Yii2 Post to controller and get back data

This is my code in view.

<?= $form->field($model, 'projectTitle')->dropDownList($fullprojectlist, [
'prompt'=>'-Choose a Course-',
'id' => 'projectList',
'type'=> 'POST',
'onchange'=>' var value = $("#projectList :selected").val();
        //$("#draftproposalform-supervisor").val(value);'


        $.post("'.Yii::$app->urlManager->createUrl(["/draftproposalform.php", "id" => value).'", function(data) {

        //set value for text box
        }
]);?>

<?= $form->field($model, 'supervisor')->textInput(['readonly'=> 'true']); ?>

I am trying to pass the selected value to the controller so that I can query the database to look for the relevant information. Then send back that information to the view to settext.

I know how to get data from the database. I just don't know how I can pass the selected value to controller and get back a value to settext while maintaining the selected value in view.

public function actionDraftproposalform() {
    $model = new DraftProposalForm();

    if($model->load(Yii::$app->request->post()) && $model->validate())
    {
        return $this->refresh();
    }

    else {

   $query = new Query;
   $query->select(['User.name', 'Project.title', 'Project.project_ID'])
        ->from('Project')
        ->innerJoin('Supervisor', '`Project`.`supervisor_ID` = `Supervisor`.`supervisor_ID`')
        ->innerJoin('User', '`Supervisor`.`user_ID` = `User`.`user_ID`')
        ->where(['Project.project_type_ID'=>1, 'Project.approval_ID'=>2]);

    $projectlist = $query->all();

    $fullprojectlist = ArrayHelper::map($projectlist, 'name', 'title', 'project_ID');


        return $this->render('draftproposalform', [
            'model'=>$model,
            'fullprojectlist' => $fullprojectlist]);
    }
}

Sorry if it's messy. Truthfully, I don't even know if passing the data back to here is the correct choice.

Edited Codes

View

<?php
        $this->registerJs(' $("#projectList").change(function() {
        var value = $("#projectList option:selected").val();
        alert(value);
        $.post(
        "'.Yii::$app->urlManager->createUrl(["/draftproposalform.php"]).'", 
        {id:value}, 
        function(data) {

            alert("Test");
            $("input[name=\'supervisor\']").val(data);
        }); });');
    ?>

        <?= $form->field($model, 'projectTitle')->dropDownList($projectlist, [
        'prompt'=>'-Choose a Course-',
        'id' => 'projectList',
        'type'=> 'POST'
        ]);
    ?>

    <?= $form->field($model, 'supervisor')->textInput(['readonly'=> 'true']); ?>

Controller

public function actionDraftproposalform() {
    $model = new DraftProposalForm();

    if(Yii::$app->request->isPost)
    {
        $id = Yii::$app->request->post("id");

        $super = DbProject::findOne(["project_ID"=>$id]);

        $supervisor = DbSupervisor::findOne(["supervisor_ID"=>$super->supervisor_ID]);

        $user = DbUser::findOne(["user_ID"=>$supervisor->user_ID]);

        //$super = DbSupervisor::findOne(["supervisor_ID"=>$id]);

    echo $user->name;

    exit;
    }


    else {


        $projectlist = ArrayHelper::map(DbProject::find()->where(['project_type_ID' => 1, 'approval_ID' => 2])->asArray()->all(), 'project_ID', 'title');

        return $this->render('draftproposalform', [
            'model'=>$model,
            'projectlist' => $projectlist]);
    }
}

Can you test this. //Controller

<?
public function actionDraftproposalform() {
    $model = new DraftProposalForm();

    if(Yii::$app->request->isPost)
    {   
        $id=Yii::$app->request->post("id");
        $super=Supervisor::findOne(["supervisor_ID"=>$id]);
        if($super) echo $super->name;else echo "Not found";exit;       
    }

    else {

   $query = new Query;
   $query->select(['User.name', 'Project.title', 'Project.project_ID'])
        ->from('Project')
        ->innerJoin('Supervisor', '`Project`.`supervisor_ID` = `Supervisor`.`supervisor_ID`')
        ->innerJoin('User', '`Supervisor`.`user_ID` = `User`.`user_ID`')
        ->where(['Project.project_type_ID'=>1, 'Project.approval_ID'=>2]);

    $projectlist = $query->all();

    $fullprojectlist = ArrayHelper::map($projectlist, 'name', 'title', 'project_ID');


        return $this->render('draftproposalform', [
            'model'=>$model,
            'fullprojectlist' => $fullprojectlist]);
    }
}

//view register js: put this in your view

<?
$this->registerJs(' $("#projectList").change(function(){
            var value = $("#projectList option:selected").val();
            $.post(
            "'.Yii::$app->urlManager->createUrl(["/draftproposalform"]).'",
             { id:value },
             function(data) {
               $( "#draftproposalform-supervisor").val(data);
            }); });');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM