title: Using Ajax to call a Yii2 function on button click
body: <p>After trying some possible solutions that I found through internet/other questions here, I can't seem to find the problem with my code. What I need is that, after clicking a button in my view, an action in my Yii2 controller gets called through Ajax and performs some action. However, with my current code, after clicking the button nothing seems to happen.</p>
  
  <p>The relevant code is as follows...</p>
  
  <p>The view:</p>
  
  <pre><code>(...)
  Html::button('Eliminar', ['data-confirm' =&gt; '¿Eliminar enunciado?', 'onclick' =&gt; '
      $.ajax({
          type: "POST",
          url: "/evaluacion/eliminar-enunciado",
          data: {
              id: '.$enunciado-&gt;id.'
          }, success: function(result) {
              if(result == 1) {
                  $.pjax.reload({container: "#construccion-evaluacion"});
              } else {
              }
          }, error: function(result) {
              console.log(\"server error\");
          }
      });
  '])
  (...)
  </code></pre>
  
  <p>The controller:</p>
  
  <pre><code>(...)
  public function actionEliminarEnunciado($id)
  {
      Enunciado::findOne($id)-&gt;delete();
      if(Enunciado::findOne($id) != null) {
          echo 1;
      } else {
          echo 0;
      }
  }
  (...)
  </code></pre>
  
  <p>Some considerations for clarification:<br>
   - The controller file is called EvaluacionController.php.<br>
   - The $enunciado->id variable is properly defined and has a valid value.</p>
  
  <p>Any advice will be appreciated. Thanks for your time!</p>