Php 将用户值与数据库进行比较,并通过ajax jquery显示结果
伙计们,我正在做我的第一个实时项目,我被困在一个点上,我需要ajax jquery的帮助。我可以用PHP实现,但我想用ajax实现 在这里,如果用户输入产品代码,那么我想将此产品代码值与我的数据库进行比较,并在我的其他表单中显示产品名称,该表单将在用户输入值后打开: 在第一个字段中,我想要产品名称: 在我的表格中,您可以看到产品代码和产品名称:Php 将用户值与数据库进行比较,并通过ajax jquery显示结果,php,jquery,ajax,database,Php,Jquery,Ajax,Database,伙计们,我正在做我的第一个实时项目,我被困在一个点上,我需要ajax jquery的帮助。我可以用PHP实现,但我想用ajax实现 在这里,如果用户输入产品代码,那么我想将此产品代码值与我的数据库进行比较,并在我的其他表单中显示产品名称,该表单将在用户输入值后打开: 在第一个字段中,我想要产品名称: 在我的表格中,您可以看到产品代码和产品名称: 我知道你想做什么,但如果没有具体的代码,我能做的就是给你一个概括的答案 当用户填写字段时,您希望将该字段发布到服务器,查找产品并返回一些内容
我知道你想做什么,但如果没有具体的代码,我能做的就是给你一个概括的答案 当用户填写字段时,您希望将该字段发布到服务器,查找产品并返回一些内容 基本情况如下所示
$(document).ready( function(){
//rolling timeout
var timeout;
$('#field').on('keyup', function(e){
if(timeout) clearTimeout(timeout);
timeout = setTimeout( function(){
var data = {
"field" : $('#field').val()
};
$.post( '{url}', data, function(response){
if(response.debug) console.log(response.debug);
if(response.success){
//open other form
$('{otherFormProductField}').val(response.product);
}
}); //end post
},450); //end timeout
});//end onKeyup
}); //end onReady
{
success : "message", //or error : "message"
debug : "",
item : ""
}
然后在PHP中,您必须处理请求。从$\u POST
数组中拉出字段
,在数据库中查找它。然后构建一个响应数组,并将其作为JSON发送回客户端。我喜欢在类似这样的结构中构建响应
$(document).ready( function(){
//rolling timeout
var timeout;
$('#field').on('keyup', function(e){
if(timeout) clearTimeout(timeout);
timeout = setTimeout( function(){
var data = {
"field" : $('#field').val()
};
$.post( '{url}', data, function(response){
if(response.debug) console.log(response.debug);
if(response.success){
//open other form
$('{otherFormProductField}').val(response.product);
}
}); //end post
},450); //end timeout
});//end onKeyup
}); //end onReady
{
success : "message", //or error : "message"
debug : "",
item : ""
}
然后在PHP中我会这样做
ob_start();
..code..
$response['debug'] = ob_get_clean();
header("Content-type:application/json");
echo json_encode($response);
这样,在开发调试信息时,您仍然可以打印调试信息(在输出缓冲区调用中),而不必担心它会弄乱Json或标头调用
-注意-使用一个超时,每次按键时重置(滚动超时)。它所做的是在每次释放钥匙时重置先前的超时。这样,它只在用户退出键入时发送请求(而不是在每次按键时发送请求)。我发现
450
毫秒大约是这个的完美值。不要太长,不要太短。基本上,一旦他们停止输入450
ms,就会触发$。post
我知道你想做什么,但如果没有具体的代码,我能做的最好的事情就是给你一个概括的答案
当用户填写字段时,您希望将该字段发布到服务器,查找产品并返回一些内容
基本情况如下所示
$(document).ready( function(){
//rolling timeout
var timeout;
$('#field').on('keyup', function(e){
if(timeout) clearTimeout(timeout);
timeout = setTimeout( function(){
var data = {
"field" : $('#field').val()
};
$.post( '{url}', data, function(response){
if(response.debug) console.log(response.debug);
if(response.success){
//open other form
$('{otherFormProductField}').val(response.product);
}
}); //end post
},450); //end timeout
});//end onKeyup
}); //end onReady
{
success : "message", //or error : "message"
debug : "",
item : ""
}
然后在PHP中,您必须处理请求。从$\u POST
数组中拉出字段
,在数据库中查找它。然后构建一个响应数组,并将其作为JSON发送回客户端。我喜欢在类似这样的结构中构建响应
$(document).ready( function(){
//rolling timeout
var timeout;
$('#field').on('keyup', function(e){
if(timeout) clearTimeout(timeout);
timeout = setTimeout( function(){
var data = {
"field" : $('#field').val()
};
$.post( '{url}', data, function(response){
if(response.debug) console.log(response.debug);
if(response.success){
//open other form
$('{otherFormProductField}').val(response.product);
}
}); //end post
},450); //end timeout
});//end onKeyup
}); //end onReady
{
success : "message", //or error : "message"
debug : "",
item : ""
}
然后在PHP中我会这样做
ob_start();
..code..
$response['debug'] = ob_get_clean();
header("Content-type:application/json");
echo json_encode($response);
这样,在开发调试信息时,您仍然可以打印调试信息(在输出缓冲区调用中),而不必担心它会弄乱Json或标头调用
-注意-使用一个超时,每次按键时重置(滚动超时)。它所做的是在每次释放钥匙时重置先前的超时。这样,它只在用户退出键入时发送请求(而不是在每次按键时发送请求)。我发现
450
毫秒大约是这个的完美值。不要太长,不要太短。基本上,一旦他们停止输入450
ms,就会触发$。post
下面是一个通用答案
JS文件:
$(document).ready(function () {
$('#myButtonId').on('click', function () {
var code = $('#myCodeInputId').val();
if (code !== '') { // checking if input is not empty
$.ajax({
url: './my/php/file.php', // php file that communicate with your DB
method: 'GET', // it could be 'POST' too
data: {code: code},
// code that will be used to find your product name
// you can call it in your php file by "$_GET['code']" if you specified GET method
dataType: 'json' // it could be 'text' too in this case
})
.done(function (response) { // on success
$('#myProductNameInput').val(response.product_name);
})
.fail(function (response) { // on error
// Handle error
});
}
});
});
// I assumed you use pdo method to communicate with your DB
try {
$dbh = new PDO('mysql:dbname=myDbName;host=myHost;charset=utf8', 'myLogin', 'myPassword');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
exit('ERROR: ' . $e->getMessage());
}
$sql = "SELECT `product_name` FROM `products` WHERE `product_code` = :code";
$result = $dbh->prepare($sql);
$result->bindValue('code', $_GET['code'], PDO::PARAM_INT);
$result->execute();
if($result->rowCount()) { // if you got a row from your DB
$row = $result->fetchObject();
echo json_encode($row, JSON_UNESCAPED_UNICODE); // as we use json method in ajax you've got to output your data this way
// if we use text method in ajax, we simply echo $row
}
else {
// handle no result case
}
PHP文件:
$(document).ready(function () {
$('#myButtonId').on('click', function () {
var code = $('#myCodeInputId').val();
if (code !== '') { // checking if input is not empty
$.ajax({
url: './my/php/file.php', // php file that communicate with your DB
method: 'GET', // it could be 'POST' too
data: {code: code},
// code that will be used to find your product name
// you can call it in your php file by "$_GET['code']" if you specified GET method
dataType: 'json' // it could be 'text' too in this case
})
.done(function (response) { // on success
$('#myProductNameInput').val(response.product_name);
})
.fail(function (response) { // on error
// Handle error
});
}
});
});
// I assumed you use pdo method to communicate with your DB
try {
$dbh = new PDO('mysql:dbname=myDbName;host=myHost;charset=utf8', 'myLogin', 'myPassword');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
exit('ERROR: ' . $e->getMessage());
}
$sql = "SELECT `product_name` FROM `products` WHERE `product_code` = :code";
$result = $dbh->prepare($sql);
$result->bindValue('code', $_GET['code'], PDO::PARAM_INT);
$result->execute();
if($result->rowCount()) { // if you got a row from your DB
$row = $result->fetchObject();
echo json_encode($row, JSON_UNESCAPED_UNICODE); // as we use json method in ajax you've got to output your data this way
// if we use text method in ajax, we simply echo $row
}
else {
// handle no result case
}
这里有一个通用的答案 JS文件:
$(document).ready(function () {
$('#myButtonId').on('click', function () {
var code = $('#myCodeInputId').val();
if (code !== '') { // checking if input is not empty
$.ajax({
url: './my/php/file.php', // php file that communicate with your DB
method: 'GET', // it could be 'POST' too
data: {code: code},
// code that will be used to find your product name
// you can call it in your php file by "$_GET['code']" if you specified GET method
dataType: 'json' // it could be 'text' too in this case
})
.done(function (response) { // on success
$('#myProductNameInput').val(response.product_name);
})
.fail(function (response) { // on error
// Handle error
});
}
});
});
// I assumed you use pdo method to communicate with your DB
try {
$dbh = new PDO('mysql:dbname=myDbName;host=myHost;charset=utf8', 'myLogin', 'myPassword');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
exit('ERROR: ' . $e->getMessage());
}
$sql = "SELECT `product_name` FROM `products` WHERE `product_code` = :code";
$result = $dbh->prepare($sql);
$result->bindValue('code', $_GET['code'], PDO::PARAM_INT);
$result->execute();
if($result->rowCount()) { // if you got a row from your DB
$row = $result->fetchObject();
echo json_encode($row, JSON_UNESCAPED_UNICODE); // as we use json method in ajax you've got to output your data this way
// if we use text method in ajax, we simply echo $row
}
else {
// handle no result case
}
PHP文件:
$(document).ready(function () {
$('#myButtonId').on('click', function () {
var code = $('#myCodeInputId').val();
if (code !== '') { // checking if input is not empty
$.ajax({
url: './my/php/file.php', // php file that communicate with your DB
method: 'GET', // it could be 'POST' too
data: {code: code},
// code that will be used to find your product name
// you can call it in your php file by "$_GET['code']" if you specified GET method
dataType: 'json' // it could be 'text' too in this case
})
.done(function (response) { // on success
$('#myProductNameInput').val(response.product_name);
})
.fail(function (response) { // on error
// Handle error
});
}
});
});
// I assumed you use pdo method to communicate with your DB
try {
$dbh = new PDO('mysql:dbname=myDbName;host=myHost;charset=utf8', 'myLogin', 'myPassword');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
exit('ERROR: ' . $e->getMessage());
}
$sql = "SELECT `product_name` FROM `products` WHERE `product_code` = :code";
$result = $dbh->prepare($sql);
$result->bindValue('code', $_GET['code'], PDO::PARAM_INT);
$result->execute();
if($result->rowCount()) { // if you got a row from your DB
$row = $result->fetchObject();
echo json_encode($row, JSON_UNESCAPED_UNICODE); // as we use json method in ajax you've got to output your data this way
// if we use text method in ajax, we simply echo $row
}
else {
// handle no result case
}
请把你的密码贴在这里。我们需要看到你在这方面的尝试欢迎来到SO。请阅读:还有@Akintunde-我不知道,因为我能读懂OP的心思@Zuber-虽然我很欣赏这种尝试,但当涉及到编码一张图片时,它并不值得千言万语。我们需要查看您拥有的html,以及您试图为此编写的任何代码。1.横切中的拼写错误(应为事务)。2.Ned Stark永远不会从Cercei购买任何东西…请在这里发布您的代码。我们需要看到你在这方面的尝试欢迎来到SO。请阅读:还有@Akintunde-我不知道,因为我能读懂OP的心思@Zuber-虽然我很欣赏这种尝试,但当涉及到编码一张图片时,它并不值得千言万语。我们需要查看您拥有的html,以及您试图为此编写的任何代码。1.横切中的拼写错误(应为事务)。2.奈德·斯塔克永远不会从塞尔西买任何东西。。我需要使用$ajax();当然可以。确保使用按钮验证产品代码。如果您不想,请阅读ArtisticPhoenix解决方案。好的,先生,谢谢,我一直在使用load()函数。我编辑了我的问题你可以看到我的代码。。我需要使用$ajax();当然可以。确保使用按钮验证产品代码。如果您不想,请阅读ArtisticPhoenix解决方案。好的,先生,谢谢,我一直在使用load()函数。我编辑了我的问题,你可以看到我的代码