Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

How do i show a list in a javascript array from a php code using a html input field?

I am trying to achieve a autocomplete html input field that gets a list of names from sql database that should be populated once clicking in the input field. I've came across a javascript autocomplete code that uses its own array but i wanted to populate its array with the sql array list instead. Would the below code work?

  <div class="autocomplete">
  <input type="text" name="customer" id="myInput" placeholder="Search Customers" value="<?php echo $CustomerSet['id']; ?>"> 
  </div>

<script>
function autocomplete(inp, arr) {

  var currentFocus;

  inp.addEventListener("input", function(e) {
  var a, b, i, val = this.value;

  closeAllLists();
  if (!val) { return false;}
  currentFocus = -1;

  a = document.createElement("DIV");
  a.setAttribute("id", this.id + "autocomplete-list");
  a.setAttribute("class", "autocomplete-items");

  this.parentNode.appendChild(a);

  for (i = 0; i < arr.length; i++) {

    if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {

      b = document.createElement("DIV");

      b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
      b.innerHTML += arr[i].substr(val.length);

      b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";

      b.addEventListener("click", function(e) {

          inp.value = this.getElementsByTagName("input")[0].value;

          closeAllLists();
      });
      a.appendChild(b);
    }
  }
  });

  inp.addEventListener("keydown", function(e) {
  var x = document.getElementById(this.id + "autocomplete-list");
  if (x) x = x.getElementsByTagName("div");
  if (e.keyCode == 40) {

    currentFocus++;

    addActive(x);
  } else if (e.keyCode == 38) { //up

    currentFocus--;

    addActive(x);
  } else if (e.keyCode == 13) {

    e.preventDefault();
    if (currentFocus > -1) {

      if (x) x[currentFocus].click();
    }
  }
  });
  function addActive(x) {

if (!x) return false;

removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);

x[currentFocus].classList.add("autocomplete-active");
  }
  function removeActive(x) {

for (var i = 0; i < x.length; i++) {
  x[i].classList.remove("autocomplete-active");
 }
 }
function closeAllLists(elmnt) {

var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
  if (elmnt != x[i] && elmnt != inp) {
    x[i].parentNode.removeChild(x[i]);
  }
}
}

document.addEventListener("click", function (e) {
  closeAllLists(e.target);
 });
}

var customers = ["<?php

                    foreach( $this->fetchCustomers[2] as $CustomerSet ) :

                        echo '<option value="'. $CustomerSet['id'] .'" ';

                        if ( $_POST['customer'] == $CustomerSet['id'] ) :

                            echo ' SELECTED ';

                        endif;

                        echo '>'. $CustomerSet['id'] .' - '. strtoupper(     $CustomerSet['customer_first'] .'  '. $CustomerSet['customer_last'] ) .'</option>';

                    endforeach;
                ?>"];
   autocomplete(document.getElementById("myInput"), customers);
   </script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like the options was formed as a single string. Try rebuilding the results in php to a simple array then using join to list in a simple array.

  <div class="autocomplete">
  <input type="text" name="customer" id="myInput" placeholder="Search Customers" value="<?php echo $CustomerSet['id']; ?>"> 
  </div>

<script>
function autocomplete(inp, arr) {

  var currentFocus;

  inp.addEventListener("input", function(e) {
  var a, b, i, val = this.value;

  closeAllLists();
  if (!val) { return false;}
  currentFocus = -1;

  a = document.createElement("DIV");
  a.setAttribute("id", this.id + "autocomplete-list");
  a.setAttribute("class", "autocomplete-items");

  this.parentNode.appendChild(a);

  for (i = 0; i < arr.length; i++) {

    if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {

      b = document.createElement("DIV");

      b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
      b.innerHTML += arr[i].substr(val.length);

      b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";

      b.addEventListener("click", function(e) {

          inp.value = this.getElementsByTagName("input")[0].value;

          closeAllLists();
      });
      a.appendChild(b);
    }
  }
  });

  inp.addEventListener("keydown", function(e) {
  var x = document.getElementById(this.id + "autocomplete-list");
  if (x) x = x.getElementsByTagName("div");
  if (e.keyCode == 40) {

    currentFocus++;

    addActive(x);
  } else if (e.keyCode == 38) { //up

    currentFocus--;

    addActive(x);
  } else if (e.keyCode == 13) {

    e.preventDefault();
    if (currentFocus > -1) {

      if (x) x[currentFocus].click();
    }
  }
  });
  function addActive(x) {

if (!x) return false;

removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);

x[currentFocus].classList.add("autocomplete-active");
  }
  function removeActive(x) {

for (var i = 0; i < x.length; i++) {
  x[i].classList.remove("autocomplete-active");
 }
 }
function closeAllLists(elmnt) {

var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
  if (elmnt != x[i] && elmnt != inp) {
    x[i].parentNode.removeChild(x[i]);
  }
}
}

document.addEventListener("click", function (e) {
  closeAllLists(e.target);
 });
}
<?php
$customers = array();
foreach( $this->fetchCustomers[2] as $CustomerSet ) {
    $selected = $_POST['customer'] == $CustomerSet['id'] ? 'SELECTED':'';
    $customers[] = '"<option value=''. addslashes($CustomerSet['id']) .'' ' . $selected . '>'. addslashes($CustomerSet['id']) .' - '. addslashes(strtoupper($CustomerSet['customer_first'])) .'  '. addslashes($CustomerSet['customer_last']) .'</option>"';
}
?>
var customers = [<?php echo join(',', $customers) ?>];
   autocomplete(document.getElementById("myInput"), customers);
   </script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...