How to call PHP function through JavaScript?
P粉133321839
2023-07-30 18:57:00
<p>I'm trying to call a PHP function from an external PHP file into a JavaScript script. My code is more complex, so I'm writing a sample code here. </p><p>This is my PHP code: </p>
<pre class="lang-php prettyprint-override"><code><?php
function add($a,$b){
$c=$a $b;
return $c;
}
function mult($a,$b){
$c=$a*$b;
return $c;
}
function divide($a,$b){
$c=$a/$b;
return $c;
}
?>
</code></pre>
<p>JS code:</p>
<pre class="brush:php;toolbar:false;"><script>
var phpadd= add(1,2); //call the php add function
var phpmult= mult(1,2); //call the php mult function
var phpdivide= divide(1,2); //call the php divide function
</script></pre>
<p>This is what I want to do. </p><p>My original PHP file did not contain these math functions, but the idea is the same. </p><p>If there is no suitable solution, can you please provide an alternative, but it should call the value from an external PHP file. </p><p><strong></strong></p>
Try this
Yes, you can send an Ajax request with data to the server using request parameters, like this (very simple):
Please note that the following code uses jQuery.
Your _functions_address.php file should look like this: