To call a php function from javascript you need to use ajax. Keep in mind jQuery needs to be included for this script to work:
PHP file foo.php
<?php
function phpPrint() {
//do something to get results;
echo $results; //it's often useful to json_encode this, if it's an array. But whatever is echoed onto the page will be availabe to ajax
//example of json_encode
echo json_encode($results);
}
Ajax script:
$.ajax({
url: 'foo.php',
method: 'POST',
data: { }, //if data needs to be sent to the php script it can be retrieved in the $_POST super global
success: function(data) { //if the ajax call was successful this method will be called and whatever data was echoed in the php script will be in the data variable
console.log(data);
var result = JSON.parse(data); //if you used json_encode for an array or object
},
error: function() { //called if the ajax method fails check link for parameters and usages
},
complete: function() { //called regardless of success or failure
}
});
You can check this out http://api.jquery.com/jquery.ajax/ to see other usages or options of the $.ajax method.
Ajax is very easy and very useful, there is no reason not to learn ajax, if you want to do web development, you need to learn it.