Home How do I adjust my AJAX code to allow for arrow key navigation?
Reply: 0

How do I adjust my AJAX code to allow for arrow key navigation?

user5024 Published in April 25, 2018, 12:23 pm

Apologies for what might appear to be an incredibly stupid question, but I'm incredibly new to AJAX. Basically, I have some code that searches a database for movie titles and provides the user with a live search as the type. They can then click on one of these results to populate the search box.

But I'd also like for the user to be able to navigate through the options with their arrow keys. I've attached my code, could anyone point me in the right direction to achieving this?

Apologies in advance that this code is very messy!


<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Movie Price Search</title>
    <link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css" integrity="sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="style.css">

    <meta http-equiv="Content-Type" content="text/html;charset=utf-16">

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $('.search-box input[type="text"]').on("keyup input", function(){
        /* Get input value on change */
        var inputVal = $(this).val();
        var resultDropdown = $(this).siblings(".result");
            $.get("backend-search.php", {term: inputVal}).done(function(data){
                // Display the returned data in browser
        } else{

    // Set search input value on click of result item
    $(document).on("click", ".result p", function(){

        function showResult(str) {
        if (str.length==0) { 
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("livesearch").style.border="1px solid #A5ACB2";

    <h1>Movie Price Search</h1>
    <div class="search-box">
        <form action="next.php" method="post" class="pure-form">
        <input type="text" autocomplete="off" placeholder="Start typing a movie title..." name="search" class="input-extended" />

        <input type="submit" name="submit" value="Search" class="pure-button button-secondary">
        <div class="result"></div>


/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */

        $servername = "localhost";
        $username = "josh";
        $password = "IYNfhkJb8SjBtj1u";
        $database = "stockdb";

$link = mysqli_connect($servername, $username, $password, $database);

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());

    // Prepare a select statement
    $sql = "SELECT * FROM stock WHERE StockName like ? LIMIT 5";

    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_term);

        // Set parameters
        $param_term = $_REQUEST['term'] . '%';

        // Attempt to execute the prepared statement
            $result = mysqli_stmt_get_result($stmt);

            // Check number of rows in the result set
            if(mysqli_num_rows($result) > 0){
                // Fetch result rows as an associative array
                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    echo "<p>" . $row["StockName"] . "</p>";
            } else{
                echo "<p>No matches found</p>";
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

    // Close statement

// close connection

Thank you in advance!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305986 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO