I have a form that accepts user input when they are trying to make payments then onclick of submit, I used jquery ajax to send to request to a server which is amount_paid_API.php, I want the amount entered by the user to be subtracted from a fixed amount in the database and to be able to get the output. am just a begginner and i don't know if to update the data when inserting it to database or to fetch the data inserted data by the user, then subtract it with the fixed value. Have tried my best am just clueless

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    </head>
    <body>
    <form method="POST" id="formPy">
    <td>
    
    <button id="mk-part-pay">Make Part Payment</button><br>
    <input type="text" name="myTution" id="myTution" class="myTutionClass" hidden>
    </td>
     <input type="submit" value="Save" class="w3-btn w3-green save" name="mysubmit">
    </form>
    </body>
    </html>
    
    <script>
    $(document).ready(function(){
      $("#mk-part-pay").click(function(e){
                e.preventDefault();
                $(".myTutionClass").toggle("fast", function(){
                    $(".myTutionClass").focus(function(){
                        $("#mk-part-pay").hide("fast");
                    })
                });
            })
    })
    
    $.ajax({
            type: "post",
            url: "http://localhost/School_App/index.php?p=payable_amnt_API",
            data: {
                tut:tut,
                submit: ""
    
            },
            dataType: "json",
            success: function (response) {
                alert(response.res);
            }
         });
    
         </script>

The PHP

<?php
         if(isset($_POST['submit'])){
    
    
    $totalArray= $_POST;
    
    foreach($totalArray as $key => $value){
        if (empty($value) || $value == ""){
            unset($totalArray[$key]);
        }
    }
    
    //print_r($totalArray);
    
    $myArr2 =array(
        "status2" => 'success2',
        "res" => 'Inserted'
    );
    $payTution = $_POST['tut'];
    $sql = "INSERT INTO payments(feestype)VALUES('$payTution')";
    myquery($sql);
    
    echo json_encode($myArr2);
    
    exit();
    }
     ?>

0

There are 0 best solutions below