select2 – search of json data binded with mysql database don’t work

By user3069176

I created a dropdown with select2 v4.0.3, it gets the data from an mysql database which works fine at the moment. My problem is that the search didn’t filter correctly and I don’t know why.

Is it because I get my data from another source?

HTML:

<select id="locations" class="js-select select2-hidden-accessible" tabindex="-1" aria-hidden="true">
<option value="0">Select a location</option>
<optgroup label="Location group">
<option value="1">Location 1</option>
<option value="2">Location 2</option>
</optgroup>
</select>
<div id="address-choosen"></div>

JS:

jQuery(".js-select").select2();
function makeAjaxRequest(countryid){
  jQuery.ajax({
      type: "POST",
      data: { countryid: countryid },
      url: "/locations.php",
      success: function(res) {
        jQuery("#address-choosen").html(res);
      }
  });
}
jQuery("#locations").on("change", function(){
   var selected = jQuery(this).val();
   makeAjaxRequest(selected);
});

PHP:

    $conn = mysqli_connect('db', 'usr', 'pw','db');
    mysqli_set_charset($conn,'utf8');
    $countryID = mysqli_real_escape_string($conn, $_POST['countryid']);
    $query = 'SELECT * FROM mkc_countries INNER JOIN mkc_contacts ON mkc_countries.contactID = mkc_contacts.contactID WHERE mkc_countries.countryID="'.$countryID.'"';
    $result = mysqli_query($conn,$query) or die('Query failed: ' . mysqli_error($conn));
    if($row = mysqli_fetch_assoc($result)){
            echo '<p><b>'.$row['company'].'</b><br />';
            echo $row['address'].'<br />';
            echo $row['city'].'<br />';
            echo $row['country'].'</p>';
            echo '<p>'.$row['phone'].'<br />';
            if(!empty($row['fax'])){
                echo $row['fax'].'</p>';
            }
            echo '<form action="/contact-us/" method="post" id="contactus">';
                echo '<input type="text" name="countryname" value="'.$row['countryName'].'" class="hidden"/>';
                echo '<input type="submit" value="Contact us" class="send" />';
            echo '</form>';
    }

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273