Javascript 如何使用select选项从数据库中获取数据,以便在inputbox中显示
用于从数据库获取数据的文件Javascript 如何使用select选项从数据库中获取数据,以便在inputbox中显示,javascript,php,html,mysql,Javascript,Php,Html,Mysql,用于从数据库获取数据的文件 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::PAR
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;
}
单击“选择选项”时,如何将数据显示到输入框中
<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" />
ID
电子邮件
日期
日期
在head中加载jquery:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
Php:
<select name="type" id="type" class="form-control">
$(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....);
}
}
});
});
return json_encode([
'status' => true
'data' => $stmt
]);