Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Ajax to inject specific PHP function using URL, or other method?
    text
    copied!<p>I'm moving from the realm of just JS to php and Ajax. I've dabbled some with PHP in the past. I <em>really</em> appreciate how much help stackoverflow has been in helping me with basic questions.</p> <p>let says I have a div called <code>#divName</code>. </p> <p>I use the following JS for Ajax. Some of this is just pseudo code.</p> <pre><code>var request = false; try { request = new XMLHttpRequest(); } catch (trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (othermicrosoft) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = false; } } } if (!request) alert("Error initializing XMLHttpRequest!"); function getAjaxInfo(&lt;name of the php function???&gt;) { //&lt;&lt;&lt;&lt;&lt; this is the function I need help with var myFunction= nameOfPHPfunctionIJustGot; var url = "filename.php?func=" + myFunction; request.open("GET", url, true); request.onreadystatechange = updatePage; request.send(null); } function updatePage() { if (request.readyState == 4) { if (request.status == 200) { var response = request.responseText; document.getElementById("divName").innerHTML = response; //probably going to use Jquery append here, but this example works. } else alert("status is " + request.status); } } </code></pre> <p>The I have my fileName.php file:</p> <pre><code>&lt;?php function header() { ?&gt; header content goes here &lt;?php } function footer() { ?&gt; footer content goes here &lt;?php } ?&gt; </code></pre> <p>My goal is that when I execute <code>getAjaxInfo()</code>, I can pull whatever PHP function I want.</p> <p>So lets say if I do a <code>onClick="getAjaxInfo(header)</code>" it will get the php header function, apply it to a javascript function, and then apply that to a div. </p> <p>Any help would be appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload