<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chris Cook .me &#187; ajax</title>
	<atom:link href="http://chriscook.me/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://chriscook.me</link>
	<description>Homepage</description>
	<lastBuildDate>Sat, 31 Jul 2010 22:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP/AJAX: Call PHP function by clicking a link</title>
		<link>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/</link>
		<comments>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 22:14:24 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[chriscook.me]]></category>
		<category><![CDATA[execute php function in ajax request]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=46</guid>
		<description><![CDATA[This tutorial demonstrates how to execute an external PHP function by clicking a simple link within HTML.  The method uses AJAX so that the page doesn&#8217;t refresh and doesn&#8217;t require a form submission. 1) Paste the following code into a .js file.  For demonstration purposes, we have named it &#8220;ajax_click.js&#8221;. /* * ajax_click.js * chriscook.me [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial demonstrates how to execute an external PHP function by clicking a simple link within HTML.  The method uses AJAX so that the page doesn&#8217;t refresh and doesn&#8217;t require a form submission.</p>
<p><strong>1)</strong> Paste the following code into a .js file.  For demonstration purposes, we have named it &#8220;ajax_click.js&#8221;.</p>
<pre class="html">
/*
* ajax_click.js
*  chriscook.me
*/
function loadurl(dest) {
try {
// Moz supports XMLHttpRequest. IE uses ActiveX.
// browser detction is bad. object detection works for any browser
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
} catch (e) {
// browser doesn&#039;t support ajax. handle however you want
}
// the xmlhttp object triggers an event everytime the status changes
// triggered() function handles the events
xmlhttp.onreadystatechange = triggered;
// open takes in the HTTP method and url.
xmlhttp.open(&quot;GET&quot;, dest);
// send the request. if this is a POST request we would have
// sent post variables: send(&quot;name=aleem gender=male)
// Moz is fine with just send(); but
// IE expects a value here, hence we do send(null);
xmlhttp.send(&quot;null&quot;);
}
function triggered() {
if ((xmlhttp.readyState == 4) (xmlhttp.status == 200)) {
document.getElementById(&quot;ajaxlink&quot;).innerHTML = xmlhttp.responseText;
}
}
</pre>
<p><strong>2)</strong> Next, add the following code in the  section of your HTML file.</p>
<pre class="html"><span class="htmlScriptTag">&lt;script src=<span class="htmlAttributeValue">&quot;ajax_link.js&quot;</span> type=<span class="htmlAttributeValue">&quot;text/javascript&quot;</span>&gt;</span><span class="htmlScriptTag">&lt;/script&gt;</span></pre>
<p><strong>3)</strong> The following code should be placed in the HTML body of a PHP file.</p>
<pre class="html">
<span class="htmlOtherTag">&lt;div id=<span class="htmlAttributeValue">&quot;ajaxlink&quot;</span> onclick=<span class="htmlAttributeValue">&quot;loadurl(&#039;ajax_function.php&#039;)&quot;</span>&gt;</span>Click Here<span class="htmlOtherTag">&lt;/div&gt;</span>
</pre>
<p>Replace &#8216;ajax_function.php&#8217; with the correct file you want to execute.  For instance, if you want your users to &#8220;click here&#8221; in order to send themselves a copy of their monthly invoice, you&#8217;d code the mailer function in &#8216;ajax_function.php&#8217;.</p>
<p><strong>4) </strong>That&#8217;s it.  Ensure that you keep the &lt;div&gt; id  as &#8220;ajaxlink&#8221;.  Once you click the link, it will disappear (until the page is refreshed).</p>
<p><strong>Feedback:</strong> I&#8217;d love to hear how you&#8217;ve used this tool.  Please feel free to post a comment on my blog.</p>
<p><strong>Disclaimer: </strong> This tutorial is provided to demonstrate how to perform the function.  Please ensure that you review the code and add security measures before using this in a production environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
