-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>
6
  • 4
    Where is your code? What have you tried to achieve that? Commented Dec 13, 2018 at 12:12
  • Here is my code: This is my form: <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"> Commented Dec 13, 2018 at 12:29
  • This is my script function used: <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> Commented Dec 13, 2018 at 12:29
  • <input value="<?= $somePresetValue ?? ''; ?>" /> ? If the value exists, autofill the form, else put '' in the value? Commented Dec 13, 2018 at 12:39
  • Please edit your question and put the code in there, in comments that is hardly readable. meta.stackoverflow.com/questions/251361/… Commented Dec 13, 2018 at 12:43

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

The code didn't worked out. Any other solution please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.