0

I am developing on top of the Program-o chatbot and for the most part, it is working great.

  1. I am trying to have the "say" input field autocomplete based on what the bot already knows from the AIML definitions.
  2. My problem is I don't know how their logic/SQL works to determine what is returned in order for me to construct the SQL for the autocomplete to function.
  3. I am using the JSON / jQuery version of the chatbot and have made no core changes to the code. Jquery and jQuery UI CDN libraries (for the autocomplete JS) have been added. The autocomplete code is from David Carr.

Thank you for any help you can provide!

//I added autocomplete JS within index.php

    $(function() {

        //autocomplete
        $(".auto").autocomplete({
            source: "search.php",
            minLength: 1
        });                

    });

//Search.php included within index.php

<?php
define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_NAME', 'demo');

if (isset($_GET['say'])){
    $return_arr = array();

    try {
        $conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname=".DB_NAME, DB_USER, DB_PASSWORD);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $conn->prepare('SELECT something FROM someTable WHERE something LIKE :say');
        $stmt->execute(array('term' => '%'.$_GET['term'].'%'));

        while($row = $stmt->fetch()) {
            $return_arr[] =  $row['something'];
        }

    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }

    /* Toss back results as json encoded array. */
    echo json_encode($return_arr);
}

?>
Jeremy Person
  • 171
  • 1
  • 3
  • 13

1 Answers1

0

I was not approaching this problem correctly. You can do a query like below. Make sure to include the jQuery UI CSS and JS files for this to work. Finally, the script below doesn't show the form itself but ensure you have an input with an id = "say" for this to work properly. Better answers are appreciated, but this is what I was able to come up with. Thank you

//Main File / index.php etc
<script>
$(function() {
    $( "#say" ).autocomplete({
       source: "search.php",
       minLength: 1
    });
});
</script>


//search.php
<?php
$host="localhost";
$username="uid";
$password="pwd";
$dbname="name";

//create a connection with dbname
$conn=mysqli_connect($host,$username,$password,$dbname);
if(!$conn)
{
 die("error in establishing connection: ". mysqli_connect_error());
}

$search=$_GET['term'];

//select query to get data from table
$sql="SELECT pattern from aiml WHERE pattern LIKE '%".$search."%'";

//run the above query
$result=mysqli_query($conn,$sql);

//display all the records
while($row=mysqli_fetch_assoc($result))
{
 //storing all the values of 'post_title' field one by one in an array
 $data[]=$row['pattern'];
}

//return json data
echo json_encode($data);
?>
Jeremy Person
  • 171
  • 1
  • 3
  • 13