简体   繁体   English

根据选择条目自动填充文本框

[英]automatically populate text box based on select entry

Is there a way within php to get a value picked up from database automatically? php中是否有一种方法可以自动从数据库中获取值?

Basically, If I have a select box & I select option "Laptop-01" , is there a way within PHP to check the database for that row and then automatically pick up the serial number of Laptop-01 and populate it within the text box for the devices serial number. 基本上,如果我有一个选择框,并且我选择了选项“ Laptop-01”,PHP内是否有一种方法可以检查该行的数据库,然后自动选择笔记本电脑01的序列号并在文本框中填充它设备序列号。

At the moment I've just got two text boxes and user would need to manually enter both the product number (Laptop-01) & then the serial number. 目前,我只有两个文本框,用户需要手动输入产品编号(Laptop-01)和序列号。

I've currently got the following code; 我目前有以下代码;

PHP 的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

<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>

Basically you want to do is : 基本上你想做的是:

When User select 'Laptop-01' you page must update all INPUTS with information related to user's laptop (like Serial number).This can be done by adding AJAX 当用户选择“ Laptop-01”时,您的页面必须用与用户笔记本电脑相关的信息(例如序列号)更新所有输入,这可以通过添加AJAX来完成

Please note : This answer will give you idea about how to Populate data from data base then you can do this for any information you needed. 请注意:此答案将使您了解如何从数据库填充数据,然后可以根据需要获取任何信息。

<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 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;    
}
}

If you want me to add more explanation on AJAX used in this page , please add request in comment section i will edit it to more better answer 如果您希望我在此页面中添加有关AJAX的更多说明,请在评论部分添加请求,我将对其进行编辑以提供更好的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM