title: Yii2 Ajax DropDownList change content of div
body: <p>I'm developing a application with Yii2 and I don't have to much knowledge of ajax. So I tried to use an example of W3Schools (<a href="http://www.w3schools.com/php/php_ajax_database.asp" rel="nofollow">http://www.w3schools.com/php/php_ajax_database.asp</a>) </p>
  
  <p>I want to make sort of a filter and I want to change the content of a div using a value from a DropDownList, here is the code of the view:</p>
  
  <pre><code>...
  &lt;script&gt;
  function showUser(str) {
    if (str==="") {
  
  document.getElementById("transactions").innerHTML="";
  return;
  } 
    if (window.XMLHttpRequest) {
  
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
   xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) {
  
    document.getElementById("transactions").innerHTML=xmlhttp.responseText;
    }
   };    
      xmlhttp.open("GET",document.location.href="filter?by="+str,true);
      xmlhttp.send();
  }
  &lt;/script&gt;
  &lt;?php $this-&gt;head() ?&gt;
  &lt;/head&gt;
  </code></pre>
  
  <p>Now the DropDownList:</p>
  
  <pre><code>&lt;?=Html::dropDownList(
      'test', //name
      'b', //select
      ['' =&gt; 'Select Filter...', '1' =&gt; 'Exchanging Now', '2' =&gt; 'Last Exchanged', '3' =&gt; 'Last Offered', '4' =&gt; 'Last Discounted', '5' =&gt; 'Last Recieved', '6' =&gt; 'View All'],
      ['onchange'=&gt; 'showUser(this.value)'])
     ?&gt;
  </code></pre>
  
  <p>Well, the content of the div "transactions" it changes but, the it redirects to localhost/.../filter?by=x</p>
  
  <p>How can I stop this redirect? In my controller I'm returning a string. Is that a problem?</p>
  
  <p>--- EDIT ---</p>
  
  <p>After some research I read about Pjax, I tried some tests and with a simple  link and with works but can't do with the dropdownlist. Here is the changes on my code.
  View:</p>
  
  <pre><code>&lt;div id="categories"&gt;
  &lt;?=Html::a('TEST LINK', Url::to(['filter', 'by' =&gt; 1]), ['data-pjax' =&gt; 'transactions'])
  ?&gt;
  </code></pre>
  
         
  
  <pre><code>    &lt;?= Html::dropDownList(
              'test', //name
              'b', //select
              ['' =&gt; 'Select Filter...', '1' =&gt; 'Exchanging Now', '2' =&gt; 'Last Exchanged', '3' =&gt; 'Last Offered', '4' =&gt; 'Last Discounted', '5' =&gt; 'Last Recieved', '6' =&gt; 'View All'], //items
              ['onchange' =&gt; 'this.form.submit()'], ['data-pjax' =&gt; 'transactions']
      )
      ?&gt;
  </code></pre>
  
  <p>
      </p>
  
  <p><a href="http://i.imgur.com/n7KcErJ.png%20Pjax%20Code" rel="nofollow">PjaxCode</a>
  StackOverflow dont let me put the code of Pjax, so I uploaded an image through a link</p>
  
  <p>Controller -  Just to show an example of what i'm returning:</p>
  
  <pre><code>public function actionFilter() {
  $by= $_POST['test'];
  $string = "";
  if ($by == 1) {
  return $string = self::searchPendingEvent();  
  }}
  
  public function searchPendingEvent() {
      $query = Event::findBySql("Select * from dot_events where (dot_event_to_user =" . Yii::$app-&gt;user-&gt;id . " "
                      . "and dot_event_type = 1 and dot_event_completed = 0) or ("
                      . "dot_event_type = 4 and dot_event_to_user =" . Yii::$app-&gt;user-&gt;id . " and dot_event_completed = 0)"
                      . " order by dot_datecreate DESC")-&gt;asArray()-&gt;all();
  
  
      $string = "";
      if ($query == null) {
          $string .="Nothing to show :(";
      } else {
  
          for ($i = 0; $i &lt; sizeof($query); $i++) {
  
              $string.= self::formatEvent($query[$i]['dot_event_id']);
          }
      }
  
      return $string;
  }
  
  public function formatEvent($id) {
  
  if ($model['dot_event_type'] == 1 &amp;&amp; $model['dot_event_to_user'] == Yii::$app-&gt;user-&gt;id) {
          $string .= "&lt;p class='text-primary text-right p1'&gt;" . Functions::xTimeAgo($model['dot_datecreate'], date("c", time())) . "&lt;/p&gt;";
          $string .="&lt;div class='well'&gt;";
          $string .="&lt;p class='lead text-left text-primary'&gt;";
          $string .= "&lt;img class='img-circle' height='50' width='50' src='" . Yii::$app-&gt;request-&gt;baseUrl . "/img/user/" . $model['dot_event_to_user'] . ".png' alt='Image Missing'&gt;";
          $string .=Html::a("You", ['user/profile']);
          $string .="&lt;/p&gt;";
          $string .="&lt;p class='text-left'&gt;requested " . Html::a($profile[0]['full_name']);
          $string .="to trade " . Html::a($tradeE['dot_cr_dots'] . " pts", ['dashboard/index']) . " from company ";
          $string .=$company['dot_com_name'] . " for company" . $companyE['dot_com_name'];
          $string .="&lt;/p&gt;";
          $string .="&lt;/div&gt;";
      }
  </code></pre>