0

I am trying to populate data from database using jquery token-input plugin http://loopj.com/jquery-tokeninput/ .but nothing shows up. I wish to know where i am going wrong.

I ACCEPT ANY ALTERNATIVE APPROACH

Every example i search does not seem to work.

My index page

<script type="text/javascript" 
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js">
</script>
<script type="text/javascript" src="src/jquery.tokeninput.js"></script>

<link rel="stylesheet" href="styles/token-input.css" type="text/css" />
<link rel="stylesheet" href="styles/token-input-facebook.css" type="text/css" />
<div>
<input type="text" id="tagcool"   name="blah" />
<input type="button" value="Submit" />       
<script type="text/javascript">
$(document).ready(function() {                                  
   $("#tagcool").tokenInput("phpsearch.php", {
   theme: "facebook",
   preventDuplicates: true,              
});

});
</script>

And my PHP page

    <?php
$link = mysqli_connect("localhost","root","","dbname") or die("Couldn't make connection.");

$look = $_GET['q']; 
$arr = array();
$rs = mysqli_query($link,"SELECT * FROM `country` WHERE name like '%".$look."%'");
# Collect the results
while($obj = mysqli_fetch_object($rs)) {
    $arr[] = $obj;
}
# JSON-encode the response
$json_response = json_encode($arr);
# Optionally: Wrap the response in a callback function for JSONP cross-domain support
if($_GET["callback"]) {
    $json_response = $_GET["callback"] . "(" . $json_response . ")";
}
# Return the response
echo $json_response;

?>

Example of my db

enter image description here

4
  • first remove extra comma in plugin options(not an answer) & explain more what exactly is not working, what is there on console & more debug info Commented Jul 17, 2016 at 14:28
  • It doesn't return any error or result. just says searching... Commented Jul 17, 2016 at 14:32
  • whats in the network tab, what does ajax status shows? can you create a jsfiddle? Commented Jul 17, 2016 at 14:33
  • Select only needed field SELECT id, name FROM country and the jQuery version is outdated, at least use 1.7+ ! Commented Jul 17, 2016 at 14:49

1 Answer 1

2

This works. You need to change a bit of the coding below according to your setup.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles/token-input-facebook.css" type="text/css" />
</head>
<body>
<form name='jqueryAutocompleteForm' id='jqueryAutocompleteForm'>
    <label for='query'>Search:</label>
    <input type='text' name='q' class='form-control input-lg' id='query' placeholder='Please Start Typing'>
    <input type='submit' value='Submit' class='btn btn-info'>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script type="text/javascript" src="src/jquery.tokeninput.js"></script> 
<script type="text/javascript">
        $(document).ready(function() {
            $("#query").tokenInput("php.php", {
                theme: "facebook"
            });
        });
        </script>
</body>
</html>

Note: Be sure the path to your "jquery.tokeninput.js" and the "php to json", in my case "php.php" and yours "phpsearch.php" is correct.

The php.php

<?php

    //open connection to mysql db
    $connection = mysqli_connect("localhost","root","","tags") or die("Error " . mysqli_error($connection));


    $s = $_GET["q"];
    //fetch table rows from mysql db
    $sql = "SELECT name from mytable WHERE name LIKE '%".$s."%'";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

    //create an array
    $emparray = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }
    echo json_encode($emparray);

    //close the db connection
    mysqli_close($connection);


?>

Replace the select query, "$sql = "SELECT name from mytable WHERE name LIKE '%".$s."%'";" with yours.

In case you need to know, my database has just 1 table with 2 columns 1.id, 2, name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.