Hi,

I have been working on trying to figure out, how to call a php function in javascript, for about two weeks.
PS: I don't have code to post because I really don't know how to implement this.

Recommended Answers

All 4 Replies

Simple script here... [Took from w3schools]

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.php",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

Use ajax to call a server side php script, which will execute the function you want.

Member Avatar for diafol

The vanilla js can be a bit intimidating if you're new to it. To get a quick result, while taking time to learn 'proper js', you could use jQuery and it's .ajax method (or its shortcut methods). Other libraries will have similar methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.