1

I want to display info and disable a process button if a certain condition is not met at the point of entering the value(onblur or onkeyup event). I have tried many example but none has given me result. Can someone help me out

The Ajax code:

<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>

Below is the php page

<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"/>

The process-loan.php script that will respond to the ajax call:

<?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
    }
}

?>
5
  • What specifically isn't working? Where are you stuck? Commented Sep 29, 2015 at 12:13
  • you have $amt = $_GET["amt"]; and an if if($amt !== NULL) but isnt your amt was comming from POST? Commented Sep 29, 2015 at 12:14
  • You are not posting any parameter amt from your ajax!! and mixing GET and POST method in your code Commented Sep 29, 2015 at 12:17
  • your key for amount is:custQual Commented Sep 29, 2015 at 12:32
  • I made a mistake, it should be $_POST and not $_GET. I want to echo qualify or not qualify when I loose focus from the amount field . Commented Sep 29, 2015 at 12:33

1 Answer 1

1

this is for demo so i have commented tour mysql related things:first change your keys in process-loan.php.

your view page:

<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"/> 

process-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);
      }
Sign up to request clarification or add additional context in comments.

6 Comments

on loading the page, the Process button is showing instead of hiding. Also, if you don't put any value in the amount field and you tab to the process button, the onblur is always showing qualify to apply.
no it will be hidden see the first line in document ready function.the message is coming because of static value in sum you can change it and use database value .
After changing it from static back to fetching from DB, it still show qualify to apply at the next tab.even for zero(0) as well. What could be wrong?Then why did u change the customerNo from fetching it from DB to iteration of numbers?
i do not have your database man .I can give demo with static values only.
your condition is if($sum >= $amt) so check what are the values in$sum and $amt.functionality you have to decide. what is zero(0) amount or sum.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.