-1

I'm trying to get autocomplete option for input form in phpdesktop-chrome which takes data from database and represents near input area while user input. The database I'm using is sqlite3 database.

HTML

<form action="actionpage.php" method="POST" class="register-form" id="register-form" autocomplete="on" >
    <label for="itemname"> Item Name</label>
    <input type="text" name="itemname" id="itemname" required placeholder="Enter item name">
    <input action="insertpage.php" type="submit" name="submit" value="Submit">
</form>

JS

<script>
    var items = [ <?php
        $result=$db->query("SELECT * FROM register WHERE itemname='$itemname'");
        while($row=$result->fetchArray(SQLITE3_ASSOC)) { 
            $item=$row['itemname'];
        } ?>
     ];
    autocomplete(document.getElementById("itemname"), items);
 </script>
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • 4
    Where is your code? What have you tried to achieve that? – B001ᛦ Dec 13 '18 at 12:12
  • Here is my code: This is my form:
    – ABHIJEET AMAN Dec 13 '18 at 12:29
  • This is my script function used: – ABHIJEET AMAN Dec 13 '18 at 12:29
  • `` ? If the value exists, autofill the form, else put '' in the value? – Jaquarh Dec 13 '18 at 12:39
  • Please edit your question and put the code in there, in comments that is hardly readable. https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks – misorude Dec 13 '18 at 12:43
  • Possible duplicate of [autocomplete from php array](https://stackoverflow.com/questions/28375173/autocomplete-from-php-array) – DollarAkshay Dec 13 '18 at 13:05

1 Answers1

0

Use my code it will work
follow this 2 steps you can get your solution
1. first use php function
2. then use JavaScript

PHP function

function pre_json($array,$view=false){
 if($view) echo '<pre>';
  echo json_encode($array, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT );
 if($view) echo '</pre>';
} 

function DBExecute($sql,$type){
  global $db;
  if($sql !='' && $type !=''){
    switch (strtolower($type) ){
      case 'select':
      $result = $db->query($sql);
      if($result){
        $resultSet = array();
        while($row = $result->fetch(PDO::FETCH_ASSOC)){
          $resultSet[] = $row;
        }
        return $resultSet;
      }else{
        echo 'Error in Selection Query <br>'.$sql.'<br>';
        $error = $db->errorInfo();
        echo '<strong>'.$error[2].'</strong>';
        exit();
      }
      break;

      default :
      return false;
    }
  } 
}

?>


Script

<script> 
        <?php  
$sql = "SELECT * FROM `register` WHERE itemname='$itemname'";
$result = DBExecute($sql,'select');

?>
var json = <?=pre_json($result);?>;
  $("#itemname").autocomplete({
    source:json,
    minLength:0,
    select:function(event,ui) {
      event.preventDefault();
      // console.log(ui.item);
      $("#itemname").val(ui.item.itemname);

    }
  }).focus(function(event) {
    $(this).autocomplete("search");
  });
</script>


then it will automatically bind in your input field

<form action="actionpage.php" method="POST" class="register-form" id="register-form" autocomplete="on" > 
    <label for="itemname"> Item Name</label> 
    <input type="text" name="itemname" id="itemname" required placeholder="Enter item name"> 
    <input action="insertpage.php" type="submit" name="submit" value="Submit">
</form>
Arul Prakash
  • 55
  • 1
  • 8