PHP说的是真的,而jQueryAjax说的是假的
我正在开发一个需要检查产品是否存在的应用程序,所以我选择了Jquery,因为它非常易于使用,但我在检查产品是否存在时遇到了一些问题 我有一个表,有一个单列,在它的td标签上有输入,它是这样工作的;您键入产品代码,然后按ENTER键检查是否存在,如果存在,则应显示一条警告,说明“存在”;如果不存在,则显示一条警告,说明“不存在”。我已经注册了一个产品,代码是“ioi90io”,所以当我按ENTER键时,会显示“notexists”,但会显示php输出,它是1,因为如果该产品存在,php会显示“echo true;”,所以php会说是,jquery会说否。为了让您更好地理解,我将代码分享给您 带有输入的html格式的我的表格:PHP说的是真的,而jQueryAjax说的是假的,php,jquery,ajax,Php,Jquery,Ajax,我正在开发一个需要检查产品是否存在的应用程序,所以我选择了Jquery,因为它非常易于使用,但我在检查产品是否存在时遇到了一些问题 我有一个表,有一个单列,在它的td标签上有输入,它是这样工作的;您键入产品代码,然后按ENTER键检查是否存在,如果存在,则应显示一条警告,说明“存在”;如果不存在,则显示一条警告,说明“不存在”。我已经注册了一个产品,代码是“ioi90io”,所以当我按ENTER键时,会显示“notexists”,但会显示php输出,它是1,因为如果该产品存在,php会显示“ec
<table class="table table-bordered">
<thead>
<tr>
<th>Codigo</th>
<th>Descripción</th>
<th>Unidades</th>
<th>Precio</th>
<th></th>
</tr>
</thead>
<tbody id="tblsurtir">
<tr class="parenttr">
<td width="20%">
<input type="text" class="form-control input-sm codigop" value="ioi909090io">
</td>
<td>
<input class="form-control input-sm campossurtir campossurtirdescr" disabled>
</td>
<td width="10%">
<input type="text" class="form-control input-sm campossurtir campossurtirunidades" disabled>
</td>
<td width="10%">
<input type="text" class="form-control input-sm campossurtir campossurtirprecio" disabled>
</td>
<td width="5%">
<button class="btn btn-danger btn-xs deleterowproduct" data-toggle="tooltip" data-placement="right" title="Borrar"><span class="glyphicon glyphicon-minus"></span></button>
</td>
</tr>
</tbody>
</table>
以及我处理ajax的php文件:
<?php
require_once "core/init.php";
if (Ajax::exists()) {
$almacen = new Almacen(new DB);
switch (Input::get("action")) {
case 'checkprodexists':
$prod = $almacen->prodExists(Input::get("codigoSeleccionado"));
if ($prod){
//this is the 1 that appears on message
echo true;
}else {
echo false;
}
break;
case 'newprod':
$nvoprod = $almacen->newProd(array(Input::get("codigoactual"),
Input::get("newDescr"),
Input::get("newUnidades"),
Input::get("newPrecio")));
if ($nvoprod) {
echo true;
}else {
echo false;
}
break;
default:
echo "ERROR";
break;
}
}
?>
所以现在我迷路了,我真的不明白出了什么问题,也许这只是我错过的一件小事,我希望你们能帮助我。你们需要了解函数的作用域。
返回true
line确实返回true,但是外部函数(comprobarExistencia
)没有显式返回任何内容,因此它只返回undefined
。您的success:function()…
行实际上只是另一个函数。因此,在这个成功处理程序中,调用另一个函数来处理返回值
类似问题的一个很好的例子可以在以下网址找到:
这个问题的核心是理解异步javascript(或一般的异步编程概念)。您不能从AJAX调用中
返回。您确实意识到comprobarExistencia
不会返回任何内容?@Musa否,用户可能不会。因此,在success函数中返回true不会返回任何内容,我不知道,对不起,jquery和ajax对我来说是新东西,如果我编写一个名为“yes”的函数,只返回true并在success函数中调用它,它会工作吗?或者,我应该怎么做?是的,在成功处理程序中调用函数将完成您希望执行的操作。但我真的建议你阅读我在下面发布的链接中投票率最高的答案。你会很高兴你这么做的!可能想提到回调…或者只是举个简单的例子。无论哪种方式,核心问题都是+1。
<?php
require_once "core/init.php";
if (Ajax::exists()) {
$almacen = new Almacen(new DB);
switch (Input::get("action")) {
case 'checkprodexists':
$prod = $almacen->prodExists(Input::get("codigoSeleccionado"));
if ($prod){
//this is the 1 that appears on message
echo true;
}else {
echo false;
}
break;
case 'newprod':
$nvoprod = $almacen->newProd(array(Input::get("codigoactual"),
Input::get("newDescr"),
Input::get("newUnidades"),
Input::get("newPrecio")));
if ($nvoprod) {
echo true;
}else {
echo false;
}
break;
default:
echo "ERROR";
break;
}
}
?>
$(document).keypress(function(e){
//this line detect when the input wich contains the code of product is "submited"
if (e.which == 13 && $(".codigop").is(":focus")) {
//var codigoSeleccionadoid = document.activeElement.id;
if(validarCodigo()) { //this only validate if the code has the right format
//NOW THIS LINE IT'S DRIVING ME CRAZY
//this function suppossed to return true beacuse product exists but that not happening
if(comprobarExistencia()) {
alert("EXISTS");
}else {
//this is the message that it shows
alert("NOT EXISTS");
}
}else {
alert("formato invalido");
}
}
});