title: Yii2 Post to controller and get back data
body: <p>This is my code in view.</p>
  
  <pre><code>&lt;?= $form-&gt;field($model, 'projectTitle')-&gt;dropDownList($fullprojectlist, [
  'prompt'=&gt;'-Choose a Course-',
  'id' =&gt; 'projectList',
  'type'=&gt; 'POST',
  'onchange'=&gt;' var value = $("#projectList :selected").val();
          //$("#draftproposalform-supervisor").val(value);'
  
  
          $.post("'.Yii::$app-&gt;urlManager-&gt;createUrl(["/draftproposalform.php", "id" =&gt; value).'", function(data) {
  
          //set value for text box
          }
  ]);?&gt;
  
  &lt;?= $form-&gt;field($model, 'supervisor')-&gt;textInput(['readonly'=&gt; 'true']); ?&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <pre><code>public function actionDraftproposalform() {
      $model = new DraftProposalForm();
  
      if($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;validate())
      {
          return $this-&gt;refresh();
      }
  
      else {
  
     $query = new Query;
     $query-&gt;select(['User.name', 'Project.title', 'Project.project_ID'])
          -&gt;from('Project')
          -&gt;innerJoin('Supervisor', '`Project`.`supervisor_ID` = `Supervisor`.`supervisor_ID`')
          -&gt;innerJoin('User', '`Supervisor`.`user_ID` = `User`.`user_ID`')
          -&gt;where(['Project.project_type_ID'=&gt;1, 'Project.approval_ID'=&gt;2]);
  
      $projectlist = $query-&gt;all();
  
      $fullprojectlist = ArrayHelper::map($projectlist, 'name', 'title', 'project_ID');
  
  
          return $this-&gt;render('draftproposalform', [
              'model'=&gt;$model,
              'fullprojectlist' =&gt; $fullprojectlist]);
      }
  }
  </code></pre>
  
  <p>Sorry if it's messy. Truthfully, I don't even know if passing the data back to here is the correct choice. </p>
  
  <p><strong>Edited Codes</strong></p>
  
  <p>View</p>
  
  <pre><code>&lt;?php
          $this-&gt;registerJs(' $("#projectList").change(function() {
          var value = $("#projectList option:selected").val();
          alert(value);
          $.post(
          "'.Yii::$app-&gt;urlManager-&gt;createUrl(["/draftproposalform.php"]).'", 
          {id:value}, 
          function(data) {
  
              alert("Test");
              $("input[name=\'supervisor\']").val(data);
          }); });');
      ?&gt;
  
          &lt;?= $form-&gt;field($model, 'projectTitle')-&gt;dropDownList($projectlist, [
          'prompt'=&gt;'-Choose a Course-',
          'id' =&gt; 'projectList',
          'type'=&gt; 'POST'
          ]);
      ?&gt;
  
      &lt;?= $form-&gt;field($model, 'supervisor')-&gt;textInput(['readonly'=&gt; 'true']); ?&gt;
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>public function actionDraftproposalform() {
      $model = new DraftProposalForm();
  
      if(Yii::$app-&gt;request-&gt;isPost)
      {
          $id = Yii::$app-&gt;request-&gt;post("id");
  
          $super = DbProject::findOne(["project_ID"=&gt;$id]);
  
          $supervisor = DbSupervisor::findOne(["supervisor_ID"=&gt;$super-&gt;supervisor_ID]);
  
          $user = DbUser::findOne(["user_ID"=&gt;$supervisor-&gt;user_ID]);
  
          //$super = DbSupervisor::findOne(["supervisor_ID"=&gt;$id]);
  
      echo $user-&gt;name;
  
      exit;
      }
  
  
      else {
  
  
          $projectlist = ArrayHelper::map(DbProject::find()-&gt;where(['project_type_ID' =&gt; 1, 'approval_ID' =&gt; 2])-&gt;asArray()-&gt;all(), 'project_ID', 'title');
  
          return $this-&gt;render('draftproposalform', [
              'model'=&gt;$model,
              'projectlist' =&gt; $projectlist]);
      }
  }
  </code></pre>