- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是屏幕
名为“fetching_book_title_for_barcode.php”的 php 文件。
<?php
require_once('db.php');
$response=array();
$sql="select book_title from book where barcode_id LIKE '%".$_POST['value']."%'";
$result=mysql_query($sql);
if(mysql_num_rows($result)){
$book_title=mysql_fetch_assoc($result);
$response["book_title1"]=$book_title;
}
echo json_encode($response);
?>
这是带有一些ajax调用的java脚本文件。
var searchTimeout;//Timer to wait a little before fetching the data
$("#barcode_id_textBox").keyup(function() {
var searchKey = this.value;
clearTimeout(searchTimeout);
searchTimeout = setTimeout(function() {
barcodeTextboxFill(searchKey);
}, 400); //If the key isn't pressed 400 ms, we fetch the data
});
function barcodeTextboxFill(searchKey){
$.ajax({
url: 'fetching_book_title_for_barcode.php',
type: 'POST',
dataType: 'json',
data: {value: searchKey},
success: function(data) {
$("#book_title_textBox").val(data);
}
});
}
HTML
<input type="text" id="barcode_id_textBox">
<input type="text" id="book_title_textBox">
`我正在根据他们的要求为我的大学制作图书馆管理系统。
在 MySql 数据库中,我有一个图书表,其中插入了图书记录,其中一列用于条形码 ID,其中一列用于图书标题。
我有一个 html 页面,上面有 2 个内联文本框,其中一个用于条形码 id,另一个用于书名。
现在我希望当输入条形码 ID 时,同时从数据库中获取书名并显示在书名的文本框中。
我尝试了很多,但没有找到任何解决方案。我通过ajax尝试过,但没有得到所需的答案。这里面有什么问题吗?
大家的帮助将不胜感激。提前致谢。
最佳答案
在文本框中添加 onChange
像这样,<input type="text" id="barcodeTextBox" onChange="BarcodeBoxFill()">
,onChange会调用JS函数BarcodeBoxFill()
每当文本框发生更改时。
然后 JS 函数应该从框中获取输入 var text = $('#barcodeTextBox').val()
然后在同一个函数上应该进行 AJAX 调用,它将文本框的值作为参数传递。然后,在页面中,AJAX 调用的 SQL 语句将获取另一个框的正确值(在本例中为书名)。作为success
AJAX 的一部分,您可以更改其他文本框的值,
success: function(data) {
$('#bookTitleTextBox').val(data);
}`
关于javascript - 在第一个文本框中输入时,在第二个文本框中获取 mysql 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41804942/
我是一名优秀的程序员,十分优秀!