如何在jquery if语句中检查ajax请求的结果的多个值

时间:2013-10-23 08:17:20

标签: php jquery ajax rows dynamically-generated

嗨,我有一个ajax请求,我通过ajax向php文件发送一些请求,并且请求生成结果到目前为止一切正常,但现在我想对我的结果保留一些验证。我在动态添加的表行上这样做,所以如果不满足验证它不会允许添加新行但仍然在验证后添加行可以任何人帮助修复我的问题谢谢 这是我的代码

  $("#savetodb").removeAttr("disabled");
  var vendor      = $("#sub_vendor_id"+rowcount+">option:selected").val();
  var product     = $("#prodid_"+rowcount).val();
  var productname = $("#prod_"+rowcount).val();
  var vouchdt     = $("#dateinfo").val();
  var qty         = $("#quantity_"+rowcount).val();
  var amt         = $("#amount_"+rowcount).val();
  $.get("../model/check_product_with_invoice_number.php", { invno : invoice, product : product, vendor : vendor, date : vouchdt, quantity : qty, amount : amt }, function (result) { 
        if(result == "") {
          alert(productname+" Does not exist for invoice number "+id);
          $("#savetodb").attr("disabled", "disabled");
       }
       if(result == 2) {
          alert("Quantity "+ qty +" for "+productname+" does not exist for invoice number "+id);
          $("#savetodb").attr("disabled", "disabled");
       }           
       if(result == 3) {
          alert("Amount "+amt +" For "+productname+" does not exist for invoice number "+id);
          $("#savetodb").attr("disabled", "disabled");
       }
if(result !=='' && result !==3 && result !==2){
$("#savetodb").removeAttr("disabled");
append_row('enable remove','',prods,unitid,unitcode);
}
});

这是我的php

$code     = mysql_real_escape_string($_GET["invno"]); 
$vendor   = mysql_real_escape_string($_GET["vendor"]); 
$product  = mysql_real_escape_string($_GET["product"]); 
$vouchdt  = mysql_real_escape_string($_GET["date"]);
$amt      = mysql_real_escape_string($_GET["amount"]);
$qty      = mysql_real_escape_string($_GET["quantity"]);


$chkquantity = mysql_query("SELECT * FROM `gc_procurement_daily_detail` WHERE quantity_procured='".$qty."'
                            AND product_id='".$product."' AND sub_vendor_id='".$vendor."'")or die(mysql_error());

if(mysql_num_rows($chkquantity)> 0){

$chkamt  = mysql_query("SELECT * FROM `gc_procurement_daily_detail` WHERE procured_detail_amount='".$amt."'
                        AND product_id='".$product."' AND sub_vendor_id='".$vendor."' AND quantity_procured='".$qty."'")or die(mysql_error());                            

if(mysql_num_rows($chkamt)>0){

$chkinvoice = mysql_query("SELECT a.*, b.* FROM `gc_procurement_daily_detail` a, `gc_procurement_daily_summary` b 
                           WHERE  a.sub_vendor_id='".$vendor."' AND a.product_id='".$product."' AND
                           a.quantity_procured='".$qty."' AND a.procured_detail_amount='".$amt."' AND
                           b.`date_of_invoice`='".$vouchdt."' AND b.invoice_number='".$code."' AND
                           a.`procurement_daily_summary_id`= b.procurement_daily_summary_id")or die(mysql_query());

$minv     = mysql_fetch_object($chkinvoice);
$fininv   = $minv->invoice_number;

if($code == $fininv){
   echo 1;  
   exit;
}else{
   echo '';
   exit;
}

}else{
   echo 3;      
   exit;
}
}else{
    echo 2;
    exit;   
}

3 个答案:

答案 0 :(得分:0)

好吧,只是扭曲双引号内的ajax条件

if(result !=='' && result !=="3" && result !=="2")

这适用于所有人。

因为这个答案被选为最佳答案 我建议结合其他想法,你应该链接你的条件

if(){
}else if(){
}

等等或使用

Switch (result){
case 1:
//Do logic of this case here
break;

case 2:
//Do logic of this case here
break;

}

因此,无论何时达到条件,您都可以退出执行

就是这样。

答案 1 :(得分:0)

对于js部分,您可以使用开关:

switch(result){
    case 2 : //error break;
    case 3 : //error break;
    case 4 : //error break;
    default: //save stuff break;
}

答案 2 :(得分:0)

使用switch语句,如下所示:

switch(result){
    case '':
        alert(productname+" Does not exist for invoice number "+id);
        $("#savetodb").attr("disabled", "disabled");
    break;
    case '2':
        alert("Quantity "+ qty +" for "+productname+" does not exist for invoice number "+id);
        $("#savetodb").attr("disabled", "disabled");
    break;
    case '3':
        alert("Amount "+amt +" For "+productname+" does not exist for invoice number "+id);
        $("#savetodb").attr("disabled", "disabled");
    break;
    default:
        $("#savetodb").removeAttr("disabled");
        append_row('enable remove','',prods,unitid,unitcode);
    break;
}

您的结果可能会以字符串而不是整数形式返回,因此您可能需要用引号括起数字。