使用加载按钮从下拉列表中填充数据

时间:2017-01-30 05:06:40

标签: php mysqli

当我从下拉列表中选择时,我正尝试使用加载按钮填充一些字段。但是,加载按钮不起作用,PHP代码看起来很好。所以我想知道哪个部分出了问题。

我想知道是否应该将加载按钮放在AuthorityCode或表单下方。但是,我尝试了两种方法,但两种方法都不起作用。

<strong>Authority Code: </strong> 
<select name=authorityid value=authorityid>Select Authority</option>
<option value = "">Select</option><br/>
    <?php

$connection = new mysqli("localhost", "username", "password", "dbname");

$stmt = $connection->prepare("SELECT AuthorityId FROM AuthorityList");

$stmt->execute();

$stmt->bind_result($authorityid);

while($stmt->fetch()){

    echo "<option value = '$authorityid'>$authorityid</option>";
}

$stmt->close();

$connection->close();

?>
</select>
<?php
if(isset($_POST["loadbtn"])){

$authorityid = $_POST["authorityid"];

$conn = new mysqli("localhost", "username", "password", "dbname");

$stmt = $conn->prepare("SELECT AuthorityName, Address, TelephoneNo, FaxNo FROM AuthorityList WHERE AuthorityId = '$authorityid'");

$stmt->execute();

   $result = mysqli_query($stmt, $conn);
    $details = mysqli_fetch_array($result);

    $savedName = $details["AuthorityName"];
    $savedAddress = $details["Address"];
    $savedTel = $details["TelephoneNo"];
    $savedFax = $details["FaxNo"];
}
// while ($row = $result->fetch_array(MYSQLI_NUM)){
// $authorityname = $row[0];
// $address = $row[1];
// $telephone = $row[2];
// $fax = $row[3];
// }

?>

<form action="" method="post" >
<input type="submit" value="Load" name="loadbtn"><br/><br/>
<strong>Authority Name: </strong> <input type="text" name="authorityname" value="<?php echo $savedName; ?>"/><br/>

<strong>Address: </strong> <input type="text" name="address" value="<?php echo $savedAddress; ?>"/><br/>

<strong>Telephone No: </strong> <input type="text" name="telephone" value="<?php echo $savedTel; ?>"/><br/>

<strong>Fax No: </strong> <input type="text" name="fax" value="<?php echo $savedFax; ?>"/><br/>

2 个答案:

答案 0 :(得分:0)

您的选择值未提交,因为它们不是表单的一部分。你的第二个查询也没有正常运行。我已经把它扯到了下面。请看一下

试试这个 -

<strong>Authority Code: </strong> 
<form action="" method="post" >
    <select name="authorityid">Select Authority
        <option value = "">Select</option><br/>
        <?php
            $connection = new mysqli("localhost", "username", "password", "dbname");
            $stmt = $connection->prepare("SELECT AuthorityId FROM AuthorityList");
            $stmt->execute();
            $stmt->bind_result($authorityid);
            while($stmt->fetch()){
                echo "<option value = '$authorityid'>$authorityid</option>";
            }
            $stmt->close();
            $connection->close();
        ?>
    </select>

    <?php
        if(isset($_POST["loadbtn"])){
            $authorityid = $_POST["authorityid"];
            $conn = new mysqli("localhost", "username", "password", "dbname");
            $qry = "SELECT AuthorityName, Address, TelephoneNo, FaxNo FROM AuthorityList WHERE AuthorityId = '$authorityid'";

            $result = $conn->query($qry);
            $details = mysqli_fetch_array($result);

            $savedName = $details["AuthorityName"];
            $savedAddress = $details["Address"];
            $savedTel = $details["TelephoneNo"];
            $savedFax = $details["FaxNo"];
        }
    ?>
    <input type="submit" value="Load" name="loadbtn"><br/><br/>
    <strong>Authority Name: </strong>
    <input type="text" name="authorityname" value="<?php echo isset($savedName) ? $savedName : ''; ?>"/>
    <br/>

    <strong>Address: </strong>
    <input type="text" name="address" value="<?php echo isset($savedAddress) ? $savedAddress : ''; ?>"/>
    <br/>

    <strong>Telephone No: </strong> 
    <input type="text" name="telephone" value="<?php echo isset($savedTel) ? $savedTel : ''; ?>"/>
    <br/>

    <strong>Fax No: </strong>
    <input type="text" name="fax" value="<?php echo isset($savedFax) ? $savedFax : ''; ?>"/>
    <br/>
</form>

您的错误似乎来自代码的mysql部分。您将在下面找到一个示例代码,该代码已从中删除了mysql部分并且正在运行 -

<!DOCTYPE html>
<html>
<head>
    <title>Fix</title>
</head>
<body>
    <strong>Authority Code: </strong> 
    <form action="" method="post" >
        <select name="authorityid">Select Authority
            <option value = "">Select</option><br/>
            <?php
                echo "<option value = '123'>123</option>";
                echo "<option value = '234'>234</option>";
                echo "<option value = '345'>345</option>";
                echo "<option value = '456'>456</option>";
            ?>
        </select>

        <?php
            if(isset($_POST["loadbtn"])){
                $authorityid = $_POST["authorityid"];
                if ($authorityid == '123') {
                    $savedName = 'nameTest';
                    $savedAddress = 'addressTest';
                    $savedTel = 'telTest';
                    $savedFax = 'faxTest';
                }

            }
        ?>
        <input type="submit" value="Load" name="loadbtn"><br/><br/>
        <strong>Authority Name: </strong>
        <input type="text" name="authorityname" value="<?php echo isset($savedName) ? $savedName : ''; ?>"/>
        <br/>

        <strong>Address: </strong>
        <input type="text" name="address" value="<?php echo isset($savedAddress) ? $savedAddress : ''; ?>"/>
        <br/>

        <strong>Telephone No: </strong> 
        <input type="text" name="telephone" value="<?php echo isset($savedTel) ? $savedTel : ''; ?>"/>
        <br/>

        <strong>Fax No: </strong>
        <input type="text" name="fax" value="<?php echo isset($savedFax) ? $savedFax : ''; ?>"/>
        <br/>
    </form>
</body>
</html>

已编辑:更改了mysqli_query函数以使其正常工作。

答案 1 :(得分:0)

将表格更改为:

<form action="window.location.reload()" method="post" >
相关问题