I thought I would share this with everyone as it is something that I have seen asked for before and there was no working examples (including the one in the tutorial) that I could get working for me.
We have a client that wants to get accurate answers from a list of 90,000 potential options ... These are trim levels in cars. So I thought the best way to do this would be to use an autocomplete first for Makes (147 of them) then to Model (7000 of them) and then the trim level ... I am working on Version 3.25.1+201124 with a MySQL database and PHP 7.4
First of all I created a table with the columns Make, Model, Trim in the database (I used a client supplied CSV file to populate this)
Next was a Short Text question with JQuery to call an external file, populate the autocomplete dropdown and stor the answer.
This was my first problem, the autocomplete.js that is included does not read the data format correctly for some reason no matter how I tried to format the JSON ... so I switched to easyautocomplete.js ([url]
easyautocomplete.com/
[/url]) and this allowed me to format the dropdowns as I wish and worked with the data. In the source of the question text I placed the following script :
<!-- JS file --><script src="YOURSERVER/lookup/jquery.easy-autocomplete.min.js"></script><!-- CSS file -->
<link href="YOURSERVER/lookup/easy-autocomplete.min.css" rel="stylesheet" />
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
var optionsName = {
url: function (data) {
return "YOURSERVER/lookup/makelookup.php?phrase=" + data;
},
list: {
maxNumberOfElements: 100,
match: {
enabled: true
}
},
getValue: "make"
};
var qID = '{QID}';
$('#question'+qID+' input[type="text"]').easyAutocomplete(optionsName);
});
</script>
Then I created a PHP file makelookup.php to read in the makes from the database and output them into a JSON data format for use in the question....
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
header('Content-Type: application/json');
$servername = 'YOURSERVER';
$dbname = 'YOURDATABASE';
$username = 'YOURUSERNAME';
$password='YOURPASSWORD!' ;
$makeRequest = $_GET;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Distinct make FROM YOURDATABASETABLE where make like '%".$makeRequest."%';";
$result = $conn->query($sql);
//echo json_encode($result);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
echo json_encode($rows);
$conn->close();
?>
This then gave me the lookup for the Make and wrote the answer into the short text question S8b.
I repeated the steps above for the model lookup filtered by the make selected with the following changes ...
The JQuery in the question source of this one I changed it to
var makeName='{S8b.NAOK}';
var optionsName = {
url: function (data) {
return "YOURSERVER/lookup/modellookup.php?make="+makeName+"&phrase=" + data;
},
and created a second php file which had the following addition and changes to it ...
Added in :
$makeRequest = $_GET["make"];
Updated :
$sql = "SELECT Distinct model FROM YOURTABLE where make like '%".$makeRequest."%' and model like '%".$modelRequest."%';";
And repeated the above for the third question adding model as a filter variable along with make to output only the Trim levels for the previously selected makes and models.
Hopefully some may find this helpful as it is something that took me a while to work out and was quite frustrating at times ...