title: Ajax + Controller Action in Yii2
body: <p>I'm new to programming, and I'm trying to call a function when the user inputs data and clicks submit button. I'm using Yii2 and I'm not familiar with Ajax. I tried developing a function, but my controller action isn't called.</p>
  
  <p>Here is the example code I'm trying:</p>
  
  <p>views/index.php:</p>
  
  <pre><code>&lt;script&gt;
      function myFunction()
      {
          $.ajax({
              url: '&lt;?php echo Yii::$app-&gt;request-&gt;baseUrl. '/supermarkets/sample' ?&gt;',
             type: 'post',
             data: {searchname: $("#searchname").val() , searchby:$("#searchby").val()},
             success: function (data) {
                alert(data);
  
             }
  
        });
      }
  &lt;/script&gt;
  
  &lt;?php
  use yii\helpers\Html;
  use yii\widgets\LinkPager;
  
  ?&gt;
  &lt;h1&gt;Supermarkets&lt;/h1&gt;
  &lt;ul&gt;
  
  &lt;select id="searchby"&gt;
      &lt;option value="" disabled="disabled" selected="selected"&gt;Search by&lt;/option&gt;
      &lt;option value="Name"&gt;Name&lt;/option&gt;
      &lt;option value="Location"&gt;Location&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;input type="text" value ="" name="searchname", id="searchname"&gt;
  &lt;button onclick="myFunction()"&gt;Search&lt;/button&gt;
  &lt;h3&gt; &lt;/h3&gt;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public function actionSample(){         
       echo "ok";
  }
  </code></pre>
  
  <p>My problem is that when I click on the Search button nothing happens, and when I try to debug it, the debugger runs no code!</p>