首页 \ 问答 \ 在ajax中验证来自外部php的动态添加的表行值(Validating dynamically added table row values from external php in ajax)

在ajax中验证来自外部php的动态添加的表行值(Validating dynamically added table row values from external php in ajax)

我正在使用一个由动态值组成的表单,例如在按钮单击时添加表行,我使用jQuery执行此操作,但现在我甚至想在将数据插入数据库之前验证我的数据。

我不得不使用外部PHP文件进行此验证我使用AJAX进行此验证,但验证工作正常,但数据仍然插入到我的数据库中。 我尽了最大努力,但仍然不适合我。

这是我的代码:

function update_db(){
           var udata = {};
           var adata = {};
           del_query = new Array();
           var confirm = 0;

        var a = "1";   
    if ($("#maintable tbody tr").length>0){

            var vendorid = $("#vendorinfo").val();  

        // prepare data to be updated
           $('[id^="dbtr_"]').each(function(index, table){

               var rid                       = $('th:eq(0)', this).find('input:eq(0)').val();
               var basecatid                 = $('td:eq(0)', this).find('select:eq(0)').val();
               var subvendorid               = $('td:eq(0)', this).find('select:eq(0)').val();
               var prodid                    = $('td:eq(1)', this).find('input:eq(1)').val();
               var productname               = $('td:eq(1)', this).find('input:eq(0)').val();
               var quantity                  = $('td:eq(2)', this).find('input:eq(0)').val();
               var muc                       = $('td:eq(3)', this).find('#muid').val();
               var amt                       = $('td:eq(4)', this).find('input:eq(0)').val();
               var reason                    = $('td:eq(5)', this).find('textarea:eq(0)').val();
               var vat_percentage            = $('td:eq(5)', this).find('input:eq(0)').val();
               var total_amount_before_vat   = $('td:eq(5)', this).find('input:eq(1)').val();
               var vat_charged_in_bill       = $('td:eq(5)', this).find('input:eq(2)').val();
               var invoice                   = $('td:eq(6)', this).find('input:eq(0)').val(); 


               if(invoice =='' && prodid !=''){
                    alert("Invoice Number Cannot Be Empty");
                    $("#savetodb").prop("disabled", true);
                    a = "0";
                    return false;
                 }


            if( quantity !='' && invoice !=''){
             var vouchdt     = $("#dateinfo").val();
                $.ajax({  
                     type: "POST",  
                     url: "../model/check_procurement_resold_with_invoice_number.php", 
                     data: { para : "upd", invno : invoice, product : prodid,  date : vouchdt, quantity : quantity},  
                     success: function(result){
                              if(result == "") {
                                alert(productname+" does not exist for invoice number "+invoice);
                                a = "0";
                                return false
                              }
                             if(result == "2") {
                                alert("Quantity "+ quantity +" for "+productname+" can't be greater than the quantity procured for invoice number "+invoice+" 1");
                                a = "0";
                                return false;
                             }

                     } 
                });
            }

            if (prodid != 'NA' && muc != '' && amt > 0 && rid != '')
            {
                if (quantity>0){
                    udata[rid] = {};
                    udata[rid]['sub_vendor_id'] = subvendorid;
                    udata[rid]['procurement_vendor_id'] = vendorid;
                    udata[rid]['product_id'] = prodid;
                    udata[rid]['quantity'] = quantity;
                    udata[rid]['measurement_unit'] = muc;
                    udata[rid]['amount_received'] = amt;
                    udata[rid]['reason'] = reason;
                    udata[rid]['vat_percentage'] = vat_percentage;
                    udata[rid]['total_amount_before_vat'] = total_amount_before_vat;
                    udata[rid]['vat_charged_in_bill'] = vat_charged_in_bill;
                    udata[rid]['invoice_number'] = invoice;
                }
                else{
                    del_query.push(rid);
                }
            }
        });
     if(a =='1'){
        $.ajax({  
             type: "POST",  
             url: "../model/bulk_procurement_resold_at_lowprice.php", 
             data: {action:'updatedb',ud:udata,ad:adata,dd:del_query,username:'<?=$gotuser?>'},  
             success: function(result){
                 results = JSON.parse(result);
                 alert('Number of records Updated : '+results['utotal_s']+"\nNumber Of records Inserted : "+results['acnt']+"\nNumber of records Deleted  : "+results['dcnt']);
            //   window.location.href="bulk_procurement_resold_at_lowprice.php?vendorinfo="+vendorid+"&dateinfo="+$("#dateinfo").val()+"&catinfo="+$("#catinfo").val();
             } 
        });
     }

这是我的php文件

$proid   = $_POST['product'];
$code    = mysql_real_escape_string($_POST["invno"]); 
$vouchdt = mysql_real_escape_string($_POST["date"]);
$qty     = mysql_real_escape_string($_POST["quantity"]);

$chkqty = mysql_query("SELECT a.quantity_procured, b.invoice_number FROM `gc_procurement_daily_detail` a, `gc_procurement_daily_summary` b 
                        WHERE  a.product_id='".$proid."'
                        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_error());   

if(mysql_num_rows($chkqty) > 0){
   $gqty =0;
   while($row = mysql_fetch_object($chkqty)){
         $mqty = $row->quantity_procured;
         $gqty = $gqty + $mqty;

    }       
    if($qty <= $gqty){
       echo 1;
    }else{
       echo 2;
    }
}else{
    echo '';   
}

I am working with a form which consist of dynamic values like adding table rows on button click and I am doing this with jQuery, but now I even want to validate my data before inserting it into my database.

I had to do this validation with external PHP file I am doing this with AJAX, but the validation is working well but still the data is being inserted into my database. I have tried my best but still its not working for me.

Here is my code:

function update_db(){
           var udata = {};
           var adata = {};
           del_query = new Array();
           var confirm = 0;

        var a = "1";   
    if ($("#maintable tbody tr").length>0){

            var vendorid = $("#vendorinfo").val();  

        // prepare data to be updated
           $('[id^="dbtr_"]').each(function(index, table){

               var rid                       = $('th:eq(0)', this).find('input:eq(0)').val();
               var basecatid                 = $('td:eq(0)', this).find('select:eq(0)').val();
               var subvendorid               = $('td:eq(0)', this).find('select:eq(0)').val();
               var prodid                    = $('td:eq(1)', this).find('input:eq(1)').val();
               var productname               = $('td:eq(1)', this).find('input:eq(0)').val();
               var quantity                  = $('td:eq(2)', this).find('input:eq(0)').val();
               var muc                       = $('td:eq(3)', this).find('#muid').val();
               var amt                       = $('td:eq(4)', this).find('input:eq(0)').val();
               var reason                    = $('td:eq(5)', this).find('textarea:eq(0)').val();
               var vat_percentage            = $('td:eq(5)', this).find('input:eq(0)').val();
               var total_amount_before_vat   = $('td:eq(5)', this).find('input:eq(1)').val();
               var vat_charged_in_bill       = $('td:eq(5)', this).find('input:eq(2)').val();
               var invoice                   = $('td:eq(6)', this).find('input:eq(0)').val(); 


               if(invoice =='' && prodid !=''){
                    alert("Invoice Number Cannot Be Empty");
                    $("#savetodb").prop("disabled", true);
                    a = "0";
                    return false;
                 }


            if( quantity !='' && invoice !=''){
             var vouchdt     = $("#dateinfo").val();
                $.ajax({  
                     type: "POST",  
                     url: "../model/check_procurement_resold_with_invoice_number.php", 
                     data: { para : "upd", invno : invoice, product : prodid,  date : vouchdt, quantity : quantity},  
                     success: function(result){
                              if(result == "") {
                                alert(productname+" does not exist for invoice number "+invoice);
                                a = "0";
                                return false
                              }
                             if(result == "2") {
                                alert("Quantity "+ quantity +" for "+productname+" can't be greater than the quantity procured for invoice number "+invoice+" 1");
                                a = "0";
                                return false;
                             }

                     } 
                });
            }

            if (prodid != 'NA' && muc != '' && amt > 0 && rid != '')
            {
                if (quantity>0){
                    udata[rid] = {};
                    udata[rid]['sub_vendor_id'] = subvendorid;
                    udata[rid]['procurement_vendor_id'] = vendorid;
                    udata[rid]['product_id'] = prodid;
                    udata[rid]['quantity'] = quantity;
                    udata[rid]['measurement_unit'] = muc;
                    udata[rid]['amount_received'] = amt;
                    udata[rid]['reason'] = reason;
                    udata[rid]['vat_percentage'] = vat_percentage;
                    udata[rid]['total_amount_before_vat'] = total_amount_before_vat;
                    udata[rid]['vat_charged_in_bill'] = vat_charged_in_bill;
                    udata[rid]['invoice_number'] = invoice;
                }
                else{
                    del_query.push(rid);
                }
            }
        });
     if(a =='1'){
        $.ajax({  
             type: "POST",  
             url: "../model/bulk_procurement_resold_at_lowprice.php", 
             data: {action:'updatedb',ud:udata,ad:adata,dd:del_query,username:'<?=$gotuser?>'},  
             success: function(result){
                 results = JSON.parse(result);
                 alert('Number of records Updated : '+results['utotal_s']+"\nNumber Of records Inserted : "+results['acnt']+"\nNumber of records Deleted  : "+results['dcnt']);
            //   window.location.href="bulk_procurement_resold_at_lowprice.php?vendorinfo="+vendorid+"&dateinfo="+$("#dateinfo").val()+"&catinfo="+$("#catinfo").val();
             } 
        });
     }

here is my php file

$proid   = $_POST['product'];
$code    = mysql_real_escape_string($_POST["invno"]); 
$vouchdt = mysql_real_escape_string($_POST["date"]);
$qty     = mysql_real_escape_string($_POST["quantity"]);

$chkqty = mysql_query("SELECT a.quantity_procured, b.invoice_number FROM `gc_procurement_daily_detail` a, `gc_procurement_daily_summary` b 
                        WHERE  a.product_id='".$proid."'
                        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_error());   

if(mysql_num_rows($chkqty) > 0){
   $gqty =0;
   while($row = mysql_fetch_object($chkqty)){
         $mqty = $row->quantity_procured;
         $gqty = $gqty + $mqty;

    }       
    if($qty <= $gqty){
       echo 1;
    }else{
       echo 2;
    }
}else{
    echo '';   
}

原文:https://stackoverflow.com/questions/19588849
更新时间:2023-11-17 20:11

最满意答案

/部分对! 细化与图像一起使用。 该对与x / y坐标相关,如

>> img: load %image.png 
== make image! [519x391 #{
1D2F9F1D2F9F1C2E9E1C2E9E1B2D9D1B2D9D1B2D9D1B2D9D1D2F9F1C2E9E
1A2C9C192B9B192B9B1A2C9C1B2D9D1C2E9E1D2EA01...
>> copy/part img 2x2
== make image! [2x2 #{
1D2F9F1D2F9F1D2F9F1D2F9F
}]

REBOL /查看图像数据类型

这里是一个例子/部分系列的例子 工作中

>> s: [a b c d e f g]
== [a b c d e f g]
>> ser: skip s 3
== [d e f g]
>> copy/part s ser
== [a b c]

The /part pair! refinement works with images. The pair relates to the x/y coordinates as in

>> img: load %image.png 
== make image! [519x391 #{
1D2F9F1D2F9F1C2E9E1C2E9E1B2D9D1B2D9D1B2D9D1B2D9D1D2F9F1C2E9E
1A2C9C192B9B192B9B1A2C9C1B2D9D1C2E9E1D2EA01...
>> copy/part img 2x2
== make image! [2x2 #{
1D2F9F1D2F9F1D2F9F1D2F9F
}]

REBOL/View Image Datatype

And here an example how /part series! is working

>> s: [a b c d e f g]
== [a b c d e f g]
>> ser: skip s 3
== [d e f g]
>> copy/part s ser
== [a b c]

相关问答

更多
  • 目前user.r已被弃用为安全风险。 应该有一种方法可以实现这种方法......但是还没有人开始研究它。 见http://chat.stackoverflow.com/transcript/291?m=9149463#9149463 Currently user.r deprecated as a security risk. There is supposed to be a dialected method for this to happen .. but no one has started wor ...
  • 根据您的需要准确。 Brett的数据网格有点基础。 例如,它本身不处理滚动条。 Henrik已经完成了具有大量功能的列表视图。 也许它可以作为你的选择: list-view 。 但是同一作者的VID扩展工具包也有不同的列表样式部分。 这是列表文档。 所有这些都是针对Rebol2的。 Depending what you need exactly. Brett's datagrid is a bit basic. For example, it does not handle scrollers by its ...
  • a)构建构建对象而不评估规范块。 这意味着规范是some [set-word! any-type!] some [set-word! any-type!]形式(如果你使用另一个对象的主体,它总会是这样)。 构建/使用第二个对象( mumble )作为原型。 b)对象操作似乎已经改变如下: i) first object被first object的words-of object替换 ii) second object被替换values-of object的values-of object iii) third ...
  • 在Rebol 2 REPL中,这应该可行。 在第一行之后,提示应该变为“继续提示”: >> if size [ [ ; 在Rebol 3中,REPL目前(2013-02)不支持多行表达式。 In the Rebol 2 REPL, this should just work. After the first line, the prompt should change into a "continuation prompt": >> if size [ [ ;
  • /部分对! 细化与图像一起使用。 该对与x / y坐标相关,如 >> img: load %image.png == make image! [519x391 #{ 1D2F9F1D2F9F1C2E9E1C2E9E1B2D9D1B2D9D1B2D9D1B2D9D1D2F9F1C2E9E 1A2C9C192B9B192B9B1A2C9C1B2D9D1C2E9E1D2EA01... >> copy/part img 2x2 == make image! [2x2 #{ 1D2F9F1D2F9F1D2F9F1D ...
  • 您可以使用set-face更新图像的方式 将刷新按钮行更改为: btn "Refresh" [set-face b img2] 或者,如果您手动更改脸部的窗格,则可以使用show (即show b ) The way you can update an image is by using set-face Change the refresh button line to: btn "Refresh" [set-face b img2] Alternatively if you are manual ...
  • 我知道的一些主题使用我的include.r ,现在在Apache 2.0下发布。 它不是模块系统,但您可能会发现它很有用。 Some subjects I know use my include.r, released under Apache 2.0 now. It is not a module system but you may find it useful.
  • 在Rebol中, rejoin和to-word都是更多元素函数的快捷方式。 在Red(从版本0.6.0开始),这两个功能都可用: >> to word! "foo" == foo >> to word! append "foo" "bar" == foobar 在附加之前复制第一个字符串可能更好,但这应该足以创建动态字。 In Rebol, both rejoin and to-word are both shortcuts for more elemental functions. In Red (as ...
  • 你正在寻找'撰写 >> parse "aaa" compose [ some (charset [#"a" #"b"] ) ] == true You're looking for 'compose >> parse "aaa" compose [ some (charset [#"a" #"b"] ) ] == true
  • 这有几种方法: x: :print ;; assign 'x to 'print x "hello world" ;; and execute it hello world blk: copy [] ;; create a block append blk :print ;; put 'print in it do [blk/1 "hello world"] ;; execute first entry in the block ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)