gpt4 book ai didi

javascript - 在第一个文本框中输入时,在第二个文本框中获取 mysql 数据

转载 作者:行者123 更新时间:2023-11-29 19:30:33 24 4
gpt4 key购买 nike

这是屏幕

enter image description here

名为“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尝试过,但没有得到所需的答案。这里面有什么问题吗?

大家的帮助将不胜感激。提前致谢。

最佳答案

使用AJAXjQuery .

在文本框中添加 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com