0

Let's say it looks like this:

<input type='text' name='customer' onkeyup='searchFor(this.value);' list='customers'>

And I have a datalist (which is embedded in PHP code which will be returned somewhere on the page)

<datalist id='customers'>
<option>$customer[0]['name']</option>
</datalist>

I want to enter a customer id and then it should return the name of the customer as a suggestion list below my text field.

That's my MySQL Query which works (var_dump() returns the correct value if entered a number)

$customer = $database->query("SELECT * FROM customers WHERE customerID LIKE ('%".$_POST["searchterm"]."%')")->fetchAll(PDO::FETCH_ASSOC);

But it doesn't show a suggestion list with my names.

In my opinion it's not working because I enter a number.

edited:

function searchFor(suchbegriff){
  var xmlHttp = null;
  if (typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
  }
  if (!xmlHttp) {
    try {
      xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xmlHttp  = null;
      }
    }
  }
  if (xmlHttp) {
    var url = "includes/search/search.php";
    var params = "suchbegriff="+suchbegriff;

    xmlHttp.open("POST", url, true);

    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");

    xmlHttp.onreadystatechange = function () {
      if (xmlHttp.readyState == 4) {
        document.getElementById("ergebnis").innerHTML = xmlHttp.responseText;
      }
    };
    xmlHttp.send(params);
  }
}

edited2

search.php

<?php
include_once("../Dbo.class.inc");
    if (isset($_POST["searchterm"])){
    $customer = $database->query("SELECT * FROM customers WHERE customerID LIKE ('%".$_POST["searchterm"]."%')")->fetchAll(PDO::FETCH_ASSOC);


        echo "<datalist id='customers'>";
        foreach($customer as $key => $value){
            foreach($value as $key2 => $value2){
                if($key2 == "Name" || $key2 == "name"){
                    echo "<option>" . $value2 . "</option>";
                }
            }
        }
        echo "</datalist>";
} ?>
6
  • Can you show your searchFor function?
    – weigreen
    Jun 5, 2016 at 11:49
  • here you go. :) It's working for other cases very well. Only for this "special" case it's not working.
    – user5600054
    Jun 5, 2016 at 11:53
  • better use prepared statements, because $_POST["searchterm"] = "'); drop table customers --" would delete your table
    – YAMM
    Jun 5, 2016 at 11:53
  • it's a web application only for internal purposes, the website won't be published..
    – user5600054
    Jun 5, 2016 at 11:55
  • how is the table-column custumerID defined? as string/varchar? or as int? can you also show search.php?
    – Jeff
    Jun 5, 2016 at 12:04

0

Your Answer

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