带数据库记录的自动完成输入

时间:2016-03-21 14:41:07

标签: php jquery mysql ajax

当有人输入名称或某些东西时,我想将我的PhpMyAdmin记录添加到自动完成功能中,它将显示要选择的数据库中的值。必须在我的输入字段中的值来自表address_state

我的代码:

<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT person_id, person_firstname, person_lastname, 
             person_email, person_phonenumber,  
             address_street,address_housenumber, 
             address_city,address_state,address_zipcode,cv_path
      FROM person 
        inner join address on address.address_id = person.person_address 
        inner join cv on cv.cv_id = person.person_cv 
          WHERE CONCAT(`person_firstname`, `person_lastname`, `address_street`, `address_housenumber`, `address_zipcode`, `address_city`, `address_state`, `person_email`, `person_phonenumber` ) 
          LIKE '%".$valueToSearch."%'";

$search_result = filterTable($query);

}
else {
$query = "SELECT person_id, person_firstname, person_lastname, 
             person_email, person_phonenumber,  
             address_street,address_housenumber, 
             address_city,address_state,address_zipcode,cv_path
      FROM person 
        inner join address on address.address_id = person.person_address 
        inner join cv on cv.cv_id = person.person_cv ";
$search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "usbw", "persons");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>Detail</title>
    <style>
        table,tr,th,td
        {
            border: 1px solid black;
        }
    </style>
    <script>
function showUser(str) {
if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
} else { 
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET","getuser.php?q="+str,true);
    xmlhttp.send();
}
}
</script>
</head>
<body>

    <form action="admin2.php" method="post" onchange="showUser(this.value)">
        <input type="text" name="valueToSearch" placeholder="Hoi" style="margin-left: 50px;">
        <input type="submit" name="search" value="Filter"><br><br>
        <div id="txtHint">
        <table>
             <tr>
                <th>Voornaam</th>
                <th>Achternaam</th>
                <th>Straat</th>
                <th>Huisnummer</th>
                <th>Postcode</th>
                <th>Stad</th>
                <th>Provincie</th>
                <th>Email</th>
                <th>Mobiel</th>
                <th>cv</th>
                <th>delete</th>
            </tr>;

  <!-- populate table from mysql database -->
            <?php while($row = mysqli_fetch_array($search_result)):?>
            <tr>
                <td><?php echo $row['person_firstname'];?></td>
                <td><?php echo $row['person_lastname'];?></td>
                <td><?php echo $row['address_street'];?></td>
                <td><?php echo $row['address_housenumber'];?></td>
                <td><?php echo $row['address_zipcode'];?></td>
                <td><?php echo $row['address_city'];?></td>
                <td><?php echo $row['address_state'];?></td>
                <td><?php echo $row['person_email'];?></td>
                <td><?php echo $row['person_phonenumber'];?></td>
                <td><?php echo "<a href='http://localhost:8080/website/" . $row['cv_path'] . "'>cv file</a>";?></td>
                <td><?php echo "<a href='delete.php?person_id=" . $row['person_id'] . "'>delete</a>";?></td>
            </tr>
            <?php endwhile;?>
        </table>
        </div>
    </form>

</body>
</html>

0 个答案:

没有答案