'How to call PHP function from within JavaScript [duplicate]

Here what I exactly need is, if I move over the HTML button, specific div tag should be reloaded without reloading whole page.



Solution 1:[1]

PHP works on the server side, and JavaScript on the client side. So to do this, you would have to make a request to the server. If you want to use plain JavaScript, take a look at Ajax:

http://www.w3schools.com/ajax/

<script>
function myPhpFunctionCall()
{
  var xmlhttp;
  xmlhttp=new XMLHttpRequest();

  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      // Do something with the results here
    }
  }
  xmlhttp.open("GET","my_function.php",true);
  xmlhttp.send();
}
</script>

Or, if you want to use jQuery, you can use their get method:

<script>
    $.get('http://yourdomain/your_script.php');
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1