douxi3432 2018-04-26 20:11
浏览 34

使用数组值填充表单并使用表单访问数据库

So I've been able to add one database field's values (Lname) to an array. I then want to use those array values to populate my form, and then pull the database entry from the specified value into a table. Is there a simpler way to do this? I'm not familiar with Jquery.

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "my_database";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    //place Last Name in array
    $sql="SELECT Lname FROM Customers";
    $result=mysqli_query($conn,$sql);

    $lnames=mysqli_fetch_all($result,MYSQLI_ASSOC);
    mysqli_free_result($result);

    foreach($lnames as $x) { 
        $a[] = $x['Lname'];
    }



    $q = intval($_GET['q']);

    $sql="SELECT * FROM Customers WHERE Lname = '".$q."'";
    $result = mysqli_query($conn,$sql);

    echo "<table>
    <tr>
    <th>Last Name</th>
    <th>Email</th>
    <th>City</th>
    <th>Balance</th>
    </tr>";

    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['Lname'] . "</td>";
        echo "<td>" . $row['Email'] . "</td>";
        echo "<td>" . $row['City'] . "</td>";
        echo "<td>" . $row['Balance'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";

    ?>

Here is the suggested script I pulled from a different Overflow forum.

<script>
function showUser(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET", "gethint.php?q=" + str, true);
        xmlhttp.send();
    }
}
</script>

And also the HTML code.

<div>
    <form>
        <select onchange="showUser(this.value)">
            <option value="">Select a person:</option>
            <option value="1">Jordan</option>
            <option value="2">Kidd</option>
            <option value="3">Hairston</option>
            <option value="4">Irving</option>
        </select>
    </form> <br>
    <div id="txtHint">
        <b>Person info will be listed here.</b>
    </div>
</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭