How to fetch data and pass. into form and response customername onkeyup or keydown json and php -


this mysql db connection.php

<?php     $conn = new mysqli("localhost", 'root', "", "laravel");     $query = mysqli_query($conn,"select * customers");      while ($result2=mysqli_fetch_assoc($query)) {       $data[] = $result2['customername'];     }     echo json_encode($data); ?> 

the json not responding onkeyup or keydown displaying whole output. want display current related matched names on display. new json , ajax. think ajax , json both respond same.

<form action="">     first name: <input type="text" id="txt1" onkeyup="showhint(this.value)">     </form>      <p>suggestions: <span id="txthint"></span></p>      <script>     function showhint(str) {       var xhttp;       if (str.length == 0) {         document.getelementbyid("txthint").innerhtml = "";         return;       }       xhttp = new xmlhttprequest();       xhttp.onreadystatechange = function() {         if (xhttp.readystate == 4 && xhttp.status == 200) {           document.getelementbyid("txthint").innerhtml = xhttp.responsetext;         }       };       xhttp.open("get", "connection.php?q="+str, true);       xhttp.send();     } </script>     

thanks suggestions. suggestions welcome.

how pass form , response on keyup or keydown , related suggestions customername should display down. new json , javascript , examples sites. in advance. suggestions welcome.

you have couple of options;


one re-pull data in ajax call appending text box input sql query in "where name %" . $input . "%'"; costly in terms of speed network traffic.


another option , 1 opt for, getting of names in select have done, using regex within javascript filter results shown.

here link on using regular expressions. https://developer.mozilla.org/en/docs/web/javascript/guide/regular_expressions


edit: none of tested don't have means test right now.

for first example, going assume passing query via , in php file change code follows:

<?php     // db connection     $conn = new mysqli("localhost", 'root', "", "laravel");     // initialise query string     $qstring = "select * customers";     // providing parameter sent in request php page, names     // containing string     isset($_get['q']) ? $qstring .= " customername '%" . $_get['q'] . "%'" : "";      $qhandler = mysqli_query($conn, $qstring);      while ($qresult = mysqli_fetch_assoc($qhandler)) {       $data[] = $qresult['customername'];     }     echo json_encode($data); ?> 

so when request made via call of showhint(str) append str query in 'like' statement, %% wildcards. put 'art' in text box, return names 'bart', 'marther', etc.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -