- Posts: 16
- Thank you received: 3
Ask the community, share ideas, and connect with other LimeSurvey users!
Start typing: <script type="text/javascript" charset="utf-8"> $(document).ready(function() { var qID = 1; $('#question'+qID+' input[type="text"]').autocomplete({ minLength: 2, // For testing: source: ["Test1","Test2","Test3"] source: 'autocomplete.php' }); }); </script>
set_include_path('/var/www/surveys/test-survey'); // Database credentials define('DB_SERVER', "localhost"); define('DB_USER', "username"); define('DB_PASSWORD', "password"); define('DB_DATABASE', "database_name"); define('DB_DRIVER', "mysql"); // Connect and query MySQL database, using PDO try { $db = new PDO(DB_DRIVER . ":dbname=" . DB_DATABASE . ";host=" . DB_SERVER . ";charset=utf8", DB_USER, DB_PASSWORD); } catch(PDOException $e) { echo $e->getMessage(); } $return_arr = array(); if ($db) { $ac_term = "%".$_GET['term']."%"; $query = "SELECT * FROM table_name WHERE column_name LIKE :term"; $result = $db->prepare($query); $result->bindValue(":term",$ac_term); $result->execute(); // Fetch data and store in array while ($row = $result->fetch(PDO::FETCH_ASSOC)) { $row_array['label'] = $row['column_name']; $row_array['value'] = $row['column_name']; array_push($return_arr,$row_array); } } // Clear query $db = null; // Encode results in JSON, for jquery autocomplete echo json_encode($return_arr);