javascript - SOLVED - How can i generate a link out of a specific result from a mysql ajax search form -


i'm trying make ajax dropdown search form suggestions based out of mysql database results. if click suggestion, links specific product.

this code i'm using that's not working:

the javascript in head of html page of form:

<script type="text/javascript"> $(document).ready(function(){ var link;     $('.search-box input[type="text"]').on("keyup input", function(){         /* input value on change */         var inputval = $(this).val();         var resultdropdown = $(this).siblings(".result");         if(inputval.length){            $.get("./includes/backend-search.php", {term: inputval}).done(function(data){             // display returned data in browser             resultdropdown.html(data);             link = data;             if (link == '<p>geen producten gevonden</p>') {                 return link = '';             } else {                 link = link.substring(3);                 link = link.substring(0, link.length-4);                 link = "#" + link             }     });         } else{             resultdropdown.empty();         }     });      // set search input value on click of result item     $(document).on("click", ".result p", function(){         $(this).parents(".search-box").find('input[type="text"]').val($(this).text());         $(this).parent(".result").empty();         console.log(link);         window.location = link;     }); }); </script> 

the search form:

<center><div class="search-box">             <input type="text" autocomplete="off" placeholder="zoek product..." />             <div class="result"></div>         </div></center> 

the backend search file runs mysql querys search suggestions

    $link = mysqli_connect($server, $gebruiker, $wachtwoord, $database);  if($link === false){     die("error: kon geen verbinding maken. " . mysqli_connect_error()); }  if(isset($_request['term'])){     $sql = "select * producten naam ?";      if($stmt = mysqli_prepare($link, $sql)){          mysqli_stmt_bind_param($stmt, "s", $param_term);          $param_term = $_request['term'] . '%';           if(mysqli_stmt_execute($stmt)){             $result = mysqli_stmt_get_result($stmt);              if(mysqli_num_rows($result) > 0){                 while($row = mysqli_fetch_array($result, mysqli_assoc)){                     echo "<p>" . $row["naam"] . "</p>";                 }             } else{                 echo "<p>geen producten gevonden</p>";             }          } else{             echo "error: kon $sql niet uitvoeren. " . mysqli_error($link);         }     }     mysqli_stmt_close($stmt); }  mysqli_close($link); 

im getting link when search pizza's:

http://localhost/index.php#pizza boromea</p><p>pizza bolognese</p><p>pizza bella italia</p><p>pizza braccio di ferro 

result clicking 1 product

while im clicking 1 product 'pizza boromea' out of suggestion list...

what im looking make work when click on example pizza boromea, redirects localhost/index.php#pizza boromea.

i hope understands problem , can me. thank already, - julian.

this problem solved using code in onclick function:

link = "#" + $(this).text(); 

by filling link variable ajax response in way, link search results not want if server returns multiple products.

since server returns each matching product <p>product name</p>, have onclick handler each <p> returned server , want link match content of clicked <p> is, can build link @ moment <p> clicked:

$(document).on("click", ".result p", function(){     $(this).parents(".search-box").find('input[type="text"]').val($(this).text());     $(this).parent(".result").empty();      // "this" refers <p> clicked     var link = "#" + $(this).text();      console.log(link);     window.location = link; }); 

note i'm defining var inside onclick handler, there's no longer need have link variable in global scope.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -