[英]How to get a data from database where i using select option so it can be shown in inputbox
用於從數據庫中獲取數據的文件
public function getOneByName($name){
try{
$link = DB_Helper::createMySQLConnection();
$query = "Select * From vendor Where vendor_name=?";
$stmt = $link->prepare($query);
$stmt->bindValue(1,$name,PDO::PARAM_STR);
}catch(PDOException $e){
$link->rollBack();
$e->getMessage();
die();
}
$link =null;
return $stmt;
}
當單擊 select 選項時,如何將數據顯示到輸入框中?
<label>ID</label><input type="text" id="id_vendor" name="id_vendor" /> <br/>
<label>Email</label><input type="text" id="email" name="email" /> <br/>
<label>Date</label><input type="date" id="start_date" name="start_date" /> <br/>
<select name="type" class="form-control">
<option selected="type" disabled=""> </option>
<?php echo $vendor ?>
</select>
<label>Date</label><input type="date" id="date_done" name="date_done" />
在頭部加載 jquery:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
在 select 字段中獲取 id:
<select name="type" id="type" class="form-control">
使用 ajax 調用獲取數據並將其顯示到輸入框中:
$(document).on('change','#type',function(e) {
e.preventDefault();
$.ajax({
url: '#Call funtion from here#',
type: 'POST',
dataType : 'json',
data: {type:$('#type').val()},
success: function(response) {
if(response.status == true) {
// Check data using console log for debug
console.log(response.data);
// Set data
$('#id_vendor').val(response.data....);
}
}
});
});
Php:
return json_encode([
'status' => true
'data' => $stmt
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.