0

I am trying to learn small concepts in PHP and MySqli.

I tried to autofill the form textbox with database value using the following script.

<!-- WITHOUT THESE THREE BELOW, THE AUTOCOMPLETE WILL LOOK UGLY OR WILL NOT WORK AT ALL -->
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>  
    $(function() {    
        $( "#tags" ).autocomplete({
            source: "autocomplete.php"
        });
    });
 </script>

And autocomplete.php (where we will get the data to fill the autocomplete input field):

<?php
    include("conn.php"); /* ESTABLISH CONNECTION IN THIS FILE; MAKE SURE THAT IT IS mysqli_* */
    $stmt = $con->prepare("SELECT description FROM table"); /* START PREPARED STATEMENT */
    $stmt->execute(); /* EXECUTE THE QUERY */
    $stmt->bind_result($description); /* BIND THE RESULT TO THIS VARIABLE */
        while($stmt->fetch()){ /* FETCH ALL RESULTS */
    $description_arr[] = $description; /* STORE EACH RESULT TO THIS VARIABLE IN ARRAY */
    } /* END OF WHILE LOOP */
    echo json_encode($description_arr); /* ECHO ALL THE RESULTS */

?>

This is a existing script in Stackoverflow.com and working fine. Below is the link.

Autocomplete Textbox results based from SQL database

But, I am not able to get the selected value id in textbox value attribute.

example in the course table php and mysqli are the two rows and ids are 1 and 2 respectively. In the autocoplete textbox if i select mysqli with the help of above script, i am expecting 2 should be in value attribute for the textbox.

please help me to get this value in textbox value attribute.

Thank you very much for the response.

Manoj
  • 23
  • 1
  • 8
  • RTM: https://jqueryui.com/autocomplete/#remote you need to add the `select:` callback. – Lawrence Cherone Sep 23 '17 at 17:24
  • Thank you very much Lawrence. I used the below code. select: function( event, ui ) { log( "Selected: " + ui.item.value + " aka " + ui.item.id ); } It did not work for me. Can you please give some more example. – Manoj Sep 23 '17 at 17:34

0 Answers0