title: Yii2: call javascript function with a button
body: <p>I want to call a javascript function from a button in php</p>
  
  <p>this is the php code:</p>
  
  <pre><code>//the view
      &lt;?php
  
      $form = yii\widgets\ActiveForm::begin(['options' =&gt; ['enctype' =&gt; 'multipart/form-data']]); 
  
      ?&gt;
  
   &lt;?=
  
  $form-&gt;field($msubs, 'id_subespecifica', ['options' =&gt; ['class' =&gt; 'col-md-12']])
    -&gt;widget(Select2::classname(),[
          'data' =&gt; ArrayHelper::map($vectorConsul,'sesp', 'sesp'),
          'options' =&gt; ['id' =&gt; 'prueba'],
          ])
  
    -&gt;label(false);
  
  
   ?&gt;
  
  
      &lt;button class="btn btn-primary" onclick="myFunction()"&gt;Add New&lt;/button&gt;
  
  
  &lt;?php
  yii\widgets\ActiveForm::end(); 
  ?&gt;
  </code></pre>
  
  <p>And this is the Javascript code:</p>
  
  <pre><code>//Javascript
  function myFunction() {
  $.ajax({
    url:'partidaasociada/get-linea',
    type:'POST',
    dataType:'json',
    data:{pruebaId:$('#prueba').val()}
    // alert(pruebaId);
  });
  </code></pre>
  
  <p>In the javascript function, i need to send the $('#prueba').val() to a php function in the controller:</p>
  
  <pre><code>//Controller
  public function actionGetLinea($pruebaId)
  {
      $model = new PartidaAsociada();
      log($pruebaId);
  }
  </code></pre>
  
  <p>But i am getting some errors, i think the button is reloading the whole form and don't recognize the previous data y sent to the form.</p>
  
  <p>Note: The 'alert()' in the javascript function is on commentary because it wont let me use, the form stay loading when i put the alert. thanks beforehand.</p>