繁体   English   中英

Ajax在使用jquery提交表单时不起作用

[英]Ajax is not working at the time form submit using jquery

我有一个表单提交页面,提交表单后,我需要调用Ajax。 但是现在Ajax无法正常工作。 我的主页是add_sale.php,Ajax页面是ajx_check_sale.php

我的代码:

add_sale.php

 function validate_form()
     {
     var cust_name= $('#cust_name').val();
     var total= $('#total').val();
      var sale_type= $('#sale_type').val();

     if(sale_type=='return')
     {
        $.ajax({
        type: "POST",
        url: 'ajx_check_sale.php',
        data:'cust_name='+cust_name + '&total=' + total,
        success: function(msg)
         { 

         alert(msg);
          /*if(msg==0)
           {

             alert("Return is greater then sale"); 
             return false;  
           } */
         }
      });
     }
    }
    <form action="" method="post" name="adFrm" onSubmit="return validate_form()">

    </form>

ajx_check_sale.php

 require_once("codelibrary/inc/variables.php");
 require_once("codelibrary/inc/functions.php");
 echo $cust_name=$_POST['cust_name'];
 echo $return=$_POST['total'];

 $cus="select sum(total) as total_sum from customer where id='$cust_id'";
 $cus2=mysql_query($cus);
 $fet=mysql_fetch_array($cus2);
 $total=$fet['total_sum'];

if($return>$total)
 {
     $status=0; 
     echo $status;  
 }
else
 {
      $status=1;    
      echo $status;     
 }

您可以更好地使用JQuery侦听器并阻止默认操作,而不是使用onSubmit调用它:

<script>

    $(document).on("submit", 'form[name="adFrm"]',function(e){

        e.preventDefault();

         var cust_name= $('#cust_name').val();
         var total= $('#total').val();
          var sale_type= $('#sale_type').val();

         if(sale_type=='return')
         {
            $.ajax({
            type: "POST",
            url: 'ajx_check_sale.php',
            data:'cust_name='+cust_name + '&total=' + total,
            success: function(msg)
             { 

             alert(msg);
              /*if(msg==0)
               {

                 alert("Return is greater then sale"); 
                 return false;  
               } */
             }
          });
         }
    });

</script>


<form action="" method="post" name="adFrm">

    <input type="submit" name="yourSubmitButton" />

</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM