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
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
You can get an idea from the below: