2016-09-22 60 views
0

在php中有沒有一種方法可以自動從數據庫中獲取值?基於選擇條目自動填充文本框

基本上,如果我有一個選擇框&我選擇選項「Laptop-01」,有沒有在PHP中檢查數據庫的行,然後自動拿起Laptop-01的序列號並填充它在設備序列號的文本框內。

目前我只有兩個文本框,用戶需要手動輸入產品編號(Laptop-01)&然後輸入序列號。

我目前得到下面的代碼;

PHP

<?php 

$selectquery = "SELECT * FROM `loanproducts`"; 
$selectresult = mysqli_query($connection, $selectquery); 

$selectusersquery = "SELECT * FROM `loanusers`"; 
$selectusersresult = mysqli_query($connection, $selectusersquery); 

if (isset($_POST['editloan'])): 

$loanid = $_POST["loanid"]; 
$username = $_POST["username"]; 
$product=$_POST["product"]; 
$product_desc=$_POST["product_desc"]; 
$serial=$_POST["serial"]; 
$date_collected=$_POST["date_collected"]; 
$date_return = $_POST["date_return"]; 
$returned = $_POST["returned"]; 



    $edit_query="UPDATE loans SET 
       username = '$username', 
       product = '$product', 
       product_desc = '$product_desc', 
       serial = '$serial', 
       date_collected ='$date_collected', 
       date_return = '$date_return', 
       returned = '$returned' 
       WHERE loanid ='$loanid'"; 
    $edit_result= mysqli_query($connection, $edit_query); 

    if ($edit_result) : 
      header ('location: editloan.php?confirm=Loan successfully updated'); 
     else : 
      echo "<b>This didn`t work, error: </b>"; 
      echo mysqli_error($connection); 
    endif; 

endif; 


$loanid=$_GET['loanid']; 

$my_query="select * from loans where loanid=$loanid"; 
$result= mysqli_query($connection, $my_query); 


while ($myrow = mysqli_fetch_array($result)): 

    $username = $myrow["username"]; 
    $product = $myrow["product"]; 
    $product_desc = $myrow["product_desc"]; 
    $serial = $myrow["serial"]; 
    $date_collected=$myrow["date_collected"]; 
    $date_return=$myrow["date_return"]; 
    $returned=$myrow["returned"]; 


endwhile; 



?> 

HTML

<html> 
<h2 align="center">Edit Product Form</h2> 
<body> 
<div id="loginp"<p align="center">Edit this loan for the Coleg Sir Gar Loan System</p></div> 
<form method="POST" action="editloaninfo.php"> 

<div id="editp"><p align="center"> 
<label class="labelform">Username:</label><select name="username" style="width: 150px"> 
    <?php while($row1 = mysqli_fetch_array($selectusersresult)) 
    { if ($row1[1] == $username) 
     $selected = "selected"; 
    else $selected = ""; 
    echo "<option $selected>{$row1[1]}</option>"; 
    }?> 
</select></p></div> 

<div id="editp"><p align="center"> 
<label class="labelform">Product:</label><select name="product" style="width: 150px"> 
    <?php while($row1 = mysqli_fetch_array($selectresult)) 
    { if ($row1[1] == $product) 
     $selected = "selected"; 
    else $selected = ""; 
    echo "<option $selected>{$row1[1]}</option>"; 
    }?> 
</select></p></div> 


<div id="editp"><p align="center"> 
<label class="labelform">Product Description:</label><input class="inputform" type="text" name="product_desc" placeholder="Product Description..." autocomplete="off" required size="18" value="<?php echo $product_desc; ?>"></p></div> 


<div id="editp"><p align="center"> 
<label class="labelform">Serial Number:</label><input class="inputform" type="text" name="serial" placeholder="Serial Number..." autocomplete="off" required size="18" value="<?php echo $serial; ?>"></p></div> 


<div id="editp"><p align="center"> 
<label class="labelform">Date Collected:</label><input class="inputform" type="date" name="date_collected" autocomplete="off" size="30" value="<?php echo $date_collected; ?>"></p></div> 


<div id="editp"><p align="center"> 
<label class="labelform">Date Returned:</label><input class="inputform" type="date" name="date_return" autocomplete="off" size="30" value="<?php echo $date_return; ?>"></p></div> 


<div id="editp"><p align="center"> 
<label class="labelform">Returned:</label><select name="returned" style="width: 150px"> 
    <option value="Yes" <?php echo $returned === 'Yes' ? 'selected="selected"' : '' ?>>Yes</option> 
    <option value="No" <?php echo $returned === 'No' ? 'selected="selected"' : '' ?>>No</option> 
</select></p></div> 

<br> 

<input type="hidden" name=loanid value= "<?php echo $loanid; ?>" > 

<div id="editp"><input class="inputform" type="submit" name="editloan" value="Save Changes"> 
<input class="inputform" type="button" name="Cancel" value="Cancel" onClick="window.location.href='editloan.php'"></div> 
</form> 
</body> 
</html> 
</div> 
+0

看起來這裏有太多的代碼,使你的問題更難閱讀。你可以創建一個[mcve]嗎? – dorukayhan

回答

0

基本上你想要做的是:

當用戶選擇 '筆記本-01',你的網頁必須更新的信息全部輸入與用戶的筆記本電腦(如序列號)。這可以通過加入AJAX

請注意:這個答案會給你想法如何填充數據庫中的數據然後你可以做到這一點你需要的任何信息。

<form> 
<select id='product_name' onchange="get_data()"> 
<option value='Laptop-01'>Laptop-01</option> 
</select> 
<input name='serial_no' id='serial_no'> 
<input name='product_no' id='product_no'> 


</form> 
<script src="js/ajax.js"></script> 
<script> 
function get_data(){ 

//In very short what this do is : 
//get values 
var serial_no = document.getElementById('product_name').value; 

//create an Object 
    var ajax = ajaxObj("POST", "yourpage.php"); 
    ajax.onreadystatechange = function() { 

     if(ajaxReturn(ajax) == true) { 

       //ajax.responsetex is things you echo from script next to it 
       var index1 = ajax.responseText.split(","); 
       //index1 is array holding values 
       //index1[0]=$serial_no 
       //index1[1]=$product_no 
       //Now set value of your serian_no INPUT to $serial_no = index1[0]; 
       document.getElementById('serial_no').value = index1[0]; 
document.getElementById('product_no').value = index1[1] 

     } 
    } 
//ajax.send = POST/GET method 
    ajax.send("product_name="+product_name); 
} 
</script> 
<?php 
if(isset($_POST['product_name'])){ 

$sql = "SELECT serial_no,product_no FROM loanuser WHERE username='$username'"; 
$query = ($connection , $query); 

while ($myrow = mysqli_fetch_array($query)){ 
$serial_no = $myrow["serial_no"]; 
$product_no = $myrow["product_no"]; 
} 
//form a (,) separated list and echo it. 
echo $serial_no.",".$product_no; 

exit(); 
} 
?> 

Ajax.js

function ajaxObj(meth, url) { 
var x = new XMLHttpRequest(); 
x.open(meth, url, true); 
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
return x; 
} 
function ajaxReturn(x){ 
if(x.readyState === 4 && x.status === 200){ 
    return true;  
} 
} 

如果你要我增加更多的解釋關於AJAX在這個頁面中使用,請加在評論部分的請求,我將編輯它更更好的答案

+0

如果此答案正確,請Upvote並標記爲答案。 。 。 –