Php 使用jquery从输入字段中的数据库获取数据
我有一个名为“reg”的数据库表。那里有许多田地。其中有两个字段是Name和ID。我想通过从选择框中选择Name来获取特定的ID。我只知道ajax适合这样做,但我没有使用ajax。如果有人愿意帮忙,那对我来说就更好了Php 使用jquery从输入字段中的数据库获取数据,php,jquery,ajax,html,Php,Jquery,Ajax,Html,我有一个名为“reg”的数据库表。那里有许多田地。其中有两个字段是Name和ID。我想通过从选择框中选择Name来获取特定的ID。我只知道ajax适合这样做,但我没有使用ajax。如果有人愿意帮忙,那对我来说就更好了 <label for="id">Instructor Name:</label><br> <select class="form-control input-sm" name="categories" id="sel3">
<label for="id">Instructor Name:</label><br>
<select class="form-control input-sm" name="categories" id="sel3">
<?php
$sql_sel = "SELECT * FROM reg WHERE Dept = '$Department' and Type !='Student' and Activity= 'Y' ";
$result_sel = mysqli_query($con,$sql_sel);
while ($row_sel = mysqli_fetch_array($result_sel)){
?>
<option value='<?php echo $row_sel["Name"]; ?>'>
<?php echo $row_sel['Name']; ?>
</option>
<?php
}
?>
</select>
<label for="id">Instructor ID:</label><br>
<input class="form-control input-sm" name="Instructor_id" id="inputsm" type="text" required>
讲师姓名:
讲师ID:
将提取的名称id分配给选择框值
然后更改select call ajax ie.get->fetchdata.php(它将返回用户数据)
然后在ajaxsuccess(data)中,您将数据作为获取值您应该将您的php代码模块化为restapi类型的实体。这称为关注点分离,并保持服务器端代码和UI代码分离。它也有助于测试驱动的开发 Jquery obj提供了可以在这里使用的.ajax()方法。 可以使用Jquery.ajax()-或-$.ajax() 所以,你可以这样做:-
$("button").click(function(){
$.ajax({url: "/path/to/rest/api", success: function(result){
$("#div1").html(result);
}});
});
我通常会建议使用Ajax,但如果您不想在代码中使用Ajax,这就是答案 您的代码应该是这样的:
function foo() {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', "/custom/custom_url");
httpRequest.send();
return httpRequest.responseText;
}
var result = foo(); // always ends up being 'undefined'
XMLHttpRequest(XHR)是JavaScript可以使用的API,
JScript、VBScript和其他web浏览器脚本语言
使用HTTP在Web服务器之间传输和处理XML数据,
在网页的
客户端和服务器端
或
你可以用这样的东西
$.getJSON
$.getJSON( '/custom/custom_url', { data1:"123", data2:"234"})
.done( function(resp){
// handle response here
}).fail(function(){
alert('Oooops');
});
使用ajax实现这一点需要帮助吗?``
<select class="form-control input-sm" name="categories" id="sel3">
<?php
$select = $con->query("SELECT * FROM reg WHERE Dept='$Department'AND Type!='Student' AND Activity='Y'");
while($row = $select->fetch_assoc()){
?>
<option value='<?php echo $row["ID"]; ?>'><?php echo $row["Name"];?></option>
<?php } ?>
</select>