0

When using Select2 on localhost (XAMPP) server to get data from the database, it works perfectly. But after deploying it to server, it doesn't work, and console shows 500 Internal Server Error.

Following is the code I'm using:

      $('#search-box').select2({
    placeholder: 'Select an item',
    ajax: {
      url: 'php/search.php',
      dataType: 'json',
      delay: 250,
      processResults: function (data) {
        return {
          results: data
        };
      },
      cache: true
    }
  });

And the PHP code in search.php:

require_once("dbinfo.php");
// Opens a connection to a MySQL server

$connection=mysqli_connect ('localhost', $username, $password);
if (!$connection) {  die('Not connected : ' . mysqli_error($connection));}

// Set the active MySQL database

$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysqli_error($connection));
}

$sql = "SELECT c_id, city_name FROM city 
    WHERE city_name LIKE '%".$_GET['q']."%'
    LIMIT 10"; 
$result = $connection->query($sql);

$json = [];
while($row = $result->fetch_assoc()){
 $json[] = ['id'=>$row['c_id'], 'text'=>$row['city_name']];
}

echo json_encode($json);

What is going wrong here?

  • A 500 error indicates a problem with your PHP. Check the web server's error logs for more details. I don't see any data being sent to the server, no query string (`?q=search_term`) on the URL or data attribute in the AJAX call (`data: { q: "search_term" }`) so the 500 error is likely because you have not defined `$_GET[q']`. – Jay Blanchard Jul 13 '17 at 12:23
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jul 13 '17 at 12:27
  • [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Jul 13 '17 at 12:28
  • But all of those things are happening on local server. I will take a look into the error logs for sure. And will also look into prepared statements. – akshaybharwani Jul 13 '17 at 12:29
  • I have included the jQuery library. There are no errors except the one I've mentioned. I'm using Apache server. – akshaybharwani Jul 13 '17 at 12:33
  • Have you watched the request / response in the browser's developer tools? Have you checked the web server's error logs? – Jay Blanchard Jul 13 '17 at 14:13
  • Log showed Syntax error where I'm declaring $json as an empty array, turns out that syntax isn't supported in PHP 5.3, so changed everything accordingly to array() function. But the error now shows, undefined index q, I'm pretty new to Ajax and Select2, and following this post: http://itsolutionstuff.com/post/jquery-select2-ajax-autocomplete-example-with-demo-in-phpexample.html . What should I do about that? – akshaybharwani Jul 14 '17 at 10:16
  • You should make the changes I suggested in my first comment. – Jay Blanchard Jul 14 '17 at 12:29

1 Answers1

0

In the ajax, set type to "GET"

ajax: {
  url: 'php/search.php',
  type: 'GET',
  dataTy.......

This works for me.

rjgodoy
  • 101
  • 4
  • 11