2017-01-30 59 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

您不能正常工作 –