使用Ajax

时间:2015-09-29 12:06:56

标签: php jquery ajax

如果在输入值(onblur或onkeyup事件)时未满足某个条件,我想显示信息并禁用进程按钮。我尝试了许多例子,但没有人给我结果。有人能帮助我吗

Ajax代码:

<script type="text/javascript" src="includes/scripts/newJquery.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("select.custid").change(function () {
            var selectedCustomer = $("#amt").val();
            var selectedCustId = $("#custid").val();
            $.ajax({
                type: "POST",
                url: "process-loan.php",
                data: {
                    custQual: selectedCustomer,
                    custid: selectedCustId
                }
            }).done(function (data) {
                $("#qualify").html(data);
            });
        });
    });
</script>

以下是php页面

<th>Customer No:</th>
<td>
    <select name="custid" class="custid" id="custid">
        <option>Select Customer No</option>
        <?php while ($rw = mysqli_fetch_array($get)) { ?>
            <option value="<?php echo $rw['custid'] ?>"><?php echo $rw['custid'] ?></option>
        <?php } ?>
    </select>
</td>
<tr>
    <th>Requesting Amount:</th>
    <td><input type="number" name="amount" value="0.00" id="amt"/></td>
</tr>
<tr>
    <td id="qualify">&nbsp;</td>
    <td id="qualify">&nbsp;</td>
</tr>
<tr>
    <td colspan="2">
        <input type="submit" name="save" value="Process Loan" class="btn btn-success" id="pButton"/>

将响应ajax调用的process-loan.php脚本:

<?php

if (isset($_POST["amt"])) {
    include 'includes/session.php';
    include 'includes/db_connection.php';

    $amt = $_GET["amt"];
    $custid = $_POST["custid"];

    // Query the Databased based on the amount and the UserId
    if ($amt !== NULL) {
        $gets = "SELECT * FROM tab_customer_dailycontribution WHERE custid='" . $custid . "' AND transactionDate BETWEEN '2015-09-01' AND '2015-09-30'";
        $get = mysqli_query($connection, $gets);
        $sum = 0.00;
        while ($row = mysqli_fetch_array($get)) {
            $sum += $row['amountContribute'];
        }

        if ($sum >= $amt) {
            //qualify for loan  $ enable the Process Button to save
            echo "You are Qualify to Apply";
        } else {
            //disqualify for loan  $ disable the process button until condition is meant.
            echo "Insufficient Fund: Unqualify to Apply";
        }
        //end if condition
    }
}

?>

1 个答案:

答案 0 :(得分:1)

这是演示所以我评论了与mysql相关的东西:首先在process-loan.php中更改你的密钥。

您的观看页面:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
     $("#pButton").hide();
    $("#amt").on("blur",function(){
       var selectedCustomer = $("#amt").val();
       var selectedCustId =  $("#custid").val();
       $.ajax({
           type: "POST",
           url:"process-loan.php",
           data:{custQual:selectedCustomer,custid:selectedCustId},
          success:function(data){
         var data=JSON.parse(data);
           $("#qualify").html(data.msg);//your message
           if(data.status == 0){
               $("#pButton").show();//showing button if qualified
           }else{
               $("#pButton").hide();
           }
       }
       });
    });
    });
</script>


    <th>Customer No:</th>
        <td><select name="custid" class="custid" id="custid">
        <option>Select Customer No</option>
        <?php $i =0; while($i <4){?>
        <option value="<?php echo $i?>"><?php echo $i?></option>
        <?php $i++;}?></select></td>
    <tr>
        <th>Requesting Amount:</th>
        <td><input type="number"  name="amount" value="0.00"  id="amt"/></td>
      </tr>
       <tr>
        <td ><div id="qualify"></div></td><!-- added div in td to show message it can be outside of your table !-->
        <td>&nbsp;</td>
      </tr>
      <tr>
      <td colspan="2"> 
     <input type="submit" name="save" value="Process Loan" class="btn btn-success"  id="pButton"/> 

<强>过程loan.php

<?php
           if(isset($_POST["custQual"])){

          //  include 'includes/session.php';
          //  include 'includes/db_connection.php';

            $amt = $_POST["custQual"];//here use $_POST not $_GET
            $custid = $_POST["custid"];

           // Query the Databased based on the amount and the UserId

             if($amt !== NULL){ 
                $sum=100000;//static value for demo
               if($sum >= $amt){
                //qualify for loan  $ enable the Process Button to save 
                $res["status"]=0;
               $res["msg"]="You are Qualify to Apply"; 
               }else{
                //disqualify for loan  $ disable the process button until condition is meant.
                $res["status"]=1;
                  $res["msg"]= "Insufficient Fund: Unqualify to Apply";   
               }//end if condition
             }else{
                $res["status"]=1;
                $res["msg"]= "put some amount first";
             }
          echo json_encode($res);
      }