2017-01-30 80 views
0

我想从下拉列表中选择时使用加载按钮填充afew字段。然而,加载按钮不工作,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/> 
+0

什么是你得到的错误? – xRahul

+0

没有错误消息,加载按钮不会填充其他字段 –

+0

尝试在''savedFax = $ details [“FaxNo”];''之后从mysql获取它们后回显变量。然后再次在'loadbtn'输入类型后使用php回显它们。你有什么? – xRahul

回答

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功能。

+0

嗨,我已经尝试过你的方法,字段仍然没有填充 –

+0

@DelwynTay你能检查你是否从查询中得到结果吗? – xRahul

+0

最初我的方法是,我能够从下拉列表中获得结果,但是在单击加载按钮时,它不会填充其他字段,即地址 –

0

变化形式:

<form action="window.location.reload()" method="post" > 
+0

+0

您不能正常工作 –