skip to Main Content

I am trying to call php function through Ajax in same php file.
User enter voucher_id then call ajax that ajax call php function that function is in same file.
How to call that function.

function of PHP code:-

function voucherExist($voucherNo, $sCID, $type){
        global $pncon;
        $uRow = $pncon->query("SELECT * FROM transactions WHERE voucher_no = '{$voucherNo}' AND company_ID = '{$sCID}' AND type = '{$type}'");
        return $uRow;
    }

Ajax code:-

<script type="text/javascript">
            $(document).ready(function (){
                $('#voucher_no').on('change', function () {
                    var voucher_no = $('#voucher_no').val();
                    $.ajax({
                        url: "bankVoucherAddEdit.php",
                        type: "post",
                        data: "voucherNo"
                    });
                });
            });
        </script>

2

Answers


  1. The thing is that you cannot directly call a PHP function from JS. You can achieve this by RESTful API. They are quite simpler and Standard.

    In your case, Send any post value as a flag from JS to PHP, if that POST value exists then call PHP function

    Login or Signup to reply.
  2. You can get an idea from the below:

    <? php 
        function yourFunction($data){
        
         return $data;
        }
    
        if (isset($_POST['voucherNo'])) {
            echo yourFunction($_POST['voucherNo']);
        }
    ?>
    
    <script>
        $.ajax({
            url: 'bankVoucherAddEdit.php',
            type: 'post',
            data: { "voucherNo": voucher_no },
            success: function(response) { console.log(response); }
        });
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search