You’ll need to call the PHP function from within the PHP page and print the result. Then connect to it with Javascript/AJAX to get the plain text the PHP prints. Example from w3schools.
<script>
function loadXMLDoc(){
var xmlhttp;
var response;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
response=xmlhttp.responseXML;
alert(response);
}
}
xmlhttp.open("GET","register.php",true);
xmlhttp.send();
}
</script>
solved calling a php function from a javascript script [duplicate]