Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用html输入字段在php代码的javascript数组中显示列表?_Javascript_Php_Html - Fatal编程技术网

如何使用html输入字段在php代码的javascript数组中显示列表?

如何使用html输入字段在php代码的javascript数组中显示列表?,javascript,php,html,Javascript,Php,Html,我试图实现一个自动完成的html输入字段,该字段从sql数据库中获取一个名称列表,单击输入字段后应填充该列表。我遇到了一个javascript自动完成代码,它使用自己的数组,但我想用sql数组列表填充它的数组。下面的代码可以工作吗 <div class="autocomplete"> <input type="text" name="customer" id="myInput" placeholder="Search Customers" value="<?php

我试图实现一个自动完成的html输入字段,该字段从sql数据库中获取一个名称列表,单击输入字段后应填充该列表。我遇到了一个javascript自动完成代码,它使用自己的数组,但我想用sql数组列表填充它的数组。下面的代码可以工作吗

  <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>


看起来选项是以单个字符串的形式形成的。尝试在php中将结果重建为一个简单数组,然后在一个简单数组中使用join to list

  <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>

var客户=[];
自动完成(document.getElementById(“myInput”),客户);

我使用jquery找到了完美的解决方案,在stackoverflow上其他人似乎也使用过jquery。我使用的解决方案是,我所做的只是对我的php/html表单代码做了一些小改动。这在很大程度上帮助了我。

需要一些关于你想要实现的目标的说明或细节。您是否试图在php中获取哈希或列表,并以javascript中解释为数组的格式打印它?这可以通过使用json_encode或简单的循环来实现。是的,我正在尝试获取一个php列表并在javascript数组中打印它。没有显示列表。我需要从var数组中获取php代码并将其与json代码显示在同一页面上吗?我不确定预期的php输出是什么。从代码来看,它看起来像是:
var客户=[“1-NAME2-NAME”]不太可能按预期工作。如果您试图生成一个字符串列表,其中每个字符串表示一个选项元素,那么我可以更新答案中的示例。是这样吗?是的,每个字符串都代表数组中的一个选项,或者我是否需要使用php echo将该数组代码添加为html表单值=“”?