I'm stuck trying to populate a search datalist with the titles stored in mySQL using jquery .post
ajax method. Problem is that can't get it to load the titles. Here is my code:
index.php
//// of couse jquery cdn link is included at header.php ////
<script>
$(document).ready(function(){
$('#datalistOptions').keyup(function(){
$.post('search_title.php',{
title:this.value,
load:5
}, function(data){
$('#datalistOptions').html(data);
});
});
});
</script>
<datalist id="datalistOptions">
</datalist>
search_title.php
<?php
include_once('config.php');
$title = $_POST['title'];
$load = $_POST['load'];
$sql = "SELECT (`movie_id`,`title`) FROM `movies` WHERE `title` LIKE '$title%' ORDER BY `title` ASC LIMIT $load";
$result = mysqli_query($conn, $sql);
$titles = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
foreach ($titles as $title) :
?>
<option data-value=".<?php echo $title['movie_id']; ?>" value="<?php echo $title['title']; ?>"></option>
<?php endforeach; ?>
######### PLEASE NOTE!! ################ I will fix the PREPARED STATEMENTS later, first I'm trying to get the AJAX to work, so pls don't mind the simple querys for the moment.