<?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>Website Design Ireland, Website Development &#187; ajax using post or get</title>
	<atom:link href="http://www.eire-webdesign.ie/blog/tag/ajax-using-post-or-get/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eire-webdesign.ie/blog</link>
	<description>Web Design and Development</description>
	<lastBuildDate>Sat, 02 Jan 2010 14:53:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Ajax load content from another file using POST or GET</title>
		<link>http://www.eire-webdesign.ie/blog/2007/10/15/ajax-load-content-from-another-file-without-browser-refresh-using-ajax/</link>
		<comments>http://www.eire-webdesign.ie/blog/2007/10/15/ajax-load-content-from-another-file-without-browser-refresh-using-ajax/#comments</comments>
		<pubDate>Mon, 15 Oct 2007 08:00:31 +0000</pubDate>
		<dc:creator>Louie @ Eire Web Design</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[ajax using post or get]]></category>

		<guid isPermaLink="false">http://www.eire-webdesign.ie/blog/2007/10/15/ajax-load-content-from-another-file-without-browser-refresh-using-ajax/</guid>
		<description><![CDATA[These is a simple Ajax example that will load content into the page from another file without browser refresh. 05-Oct-2007 &#8211; We have updated the javascript to allow the use of forms to be submited using POST. Click here for Demo » OR DOWNLOAD THE AJAX SAMPLE How does it work? First we need to create [...]]]></description>
			<content:encoded><![CDATA[<p>These is a simple Ajax example that will load content into the page from another file without browser refresh.<br />
05-Oct-2007 &#8211; We have updated the javascript to allow the use of forms to be submited using POST.</p>
<p><a title="Ajax sample" href="http://www.eire-webdesign.ie/ajax_samples/index.php" target="_blank"><strong><span style="font-size: medium;">Click here for Demo »</span></strong></a> OR <a href="http://www.eire-webdesign.ie/ajax_samples/ajax_samples.rar"><strong><span style="font-size: medium;">DOWNLOAD THE AJAX SAMPLE</span></strong></a></p>
<p><strong>How does it work?<br />
</strong><br />
First we need to create the javascript file with the following code:</p>
<pre lang="javascript">var ewd_domain = 'http://www.eire-webdesign.ie/'; //set domain
var ewd_loading_img = ewd_domain + 'images/loading.gif';//set image path
var ewd_loading_msg = '<strong>Loading Data...</strong>';//set loading message
var xmlhttp_obj = false;       

//create the XMLHttpRequest
function ewd_xmlhttp(){
if (window.XMLHttpRequest){ // if Mozilla, Safari etc
xmlhttp_obj = new XMLHttpRequest();
}else if (window.ActiveXObject){ // if IE
try{
xmlhttp_obj = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlhttp_obj = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
}
}    

}else{
xmlhttp_obj = false;
}
return xmlhttp_obj;
}    

//get content via GET
function ewd_getcontent(url, containerid){
var xmlhttp_obj = ewd_xmlhttp();
document.getElementById(containerid).innerHTML = '<img src="http://www.eire-webdesign.ie/blog/wp-admin/'%20+%20ewd_loading_img%20+%20'" alt="" />' + ewd_loading_msg;
xmlhttp_obj.onreadystatechange=function(){
ewd_loadpage(xmlhttp_obj, '', containerid);
}
xmlhttp_obj.open('GET', url, true);
xmlhttp_obj.send(null);
}       

function ewd_loadpage(xmlhttp_obj, content, containerid){
if ( xmlhttp_obj.readyState == 4 &amp;&amp; xmlhttp_obj.status == 200 ){
document.getElementById(containerid).innerHTML = xmlhttp_obj.responseText;
}
}       

//functions for posted values from forms vis POST
function ewd_getcontent_post(url, content, containerid){
var xinput = content;
var xmlhttp_obj = ewd_xmlhttp();
document.getElementById(containerid).innerHTML = '<img src="http://www.eire-webdesign.ie/blog/wp-admin/'%20+%20ewd_loading_img%20+%20'" alt="" />' + ewd_loading_msg;
xmlhttp_obj.open('POST', url, true);
xmlhttp_obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp_obj.onreadystatechange = function() {
ewd_loadpage(xmlhttp_obj, content, containerid);
}
xmlhttp_obj.send(content);
}   

//convert form data sent to POST CONTENT
function ewd_submit_form(page_to,form_name,containerid) {
var content = ewd_convertFormDataToPostContent(form_name);
ewd_getcontent_post(page_to, content, containerid);
}       

function ewd_convertFormDataToPostContent(form_y){
var f=document.getElementById(form_y);
var content_to_submit = '';
var form_element;
var last_element_name = '';
for (i = 0; i &lt; f.elements.length; i++){
form_element = f.elements[i];
switch (form_element.type){
// text fields, hidden form elements
case 'text':
case 'hidden':
case 'password':
case 'textarea':
case 'select-one':
content_to_submit += form_element.name + '=' + escape(form_element.value) + '&amp;';
break;
// radio buttons
case 'radio':
if (form_element.checked){
content_to_submit += form_element.name + '=' + escape(form_element.value) + '&amp;';
}
break;
// checkboxes
case 'checkbox':
if (form_element.checked){
// Continuing multiple, same-name checkboxes
if (form_element.name == last_element_name){
// Strip of end ampersand if there is one
if (content_to_submit.lastIndexOf('&amp;') == content_to_submit.length - 1){
content_to_submit = content_to_submit.substr(0, content_to_submit.length - 1);
}
// Append value as comma-delimited string
content_to_submit += ',' + escape(form_element.value);
}else{
content_to_submit += form_element.name + '=' + escape(form_element.value);
}
content_to_submit += '&amp;';
last_element_name = form_element.name;
}
break;
}//end switch
} //end for
// remove trailing separator
content_to_submit = content_to_submit.substr(0, content_to_submit.length - 1);
return content_to_submit;
}</pre>
<p>Now create the page that will hold the content to be displayed (&#8220;content_ajax.php&#8221;):</p>
<pre lang="php">	First we create the link that "<strong>onclick</strong>" will populate the div or span id - "div_populate".
	Make sure you have the javascript source "<strong>ajax.js</strong>" included into this file.

	<a onclick="ewd_getcontent('content_ajax.php?data=1', 'div_populate'); return false" href="index.php">• Get Data 1 »</a>
	  
	<a onclick="ewd_getcontent('content_ajax.php?data=2', 'div_populate'); return false" href="index.php">• Get Data 2 »</a>
<form id="formx" enctype="application/x-www-form-urlencoded" method="post">
	<strong>or you can use a form to submit data (using "onchange" on the form input or "onclick" on the submit button):</strong>
<input id="test" name="test" size="100" type="text" />
<input id="test1" name="test1" size="100" type="text" />
<input id="test2" onclick="ewd_submit_form('content_ajax.php','formx','div_populate');" name="test2" type="checkbox" value="on" />
<input onclick="ewd_submit_form('content_ajax.php','formx','div_populate'); return false;" type="submit" value="Submit Query" />
	</form>

Now and example using a form to post the data

	Here is the place where the data from the "<strong>content_ajax.php</strong>" page will be displayed.

		©<!--p echo date("Y")--> <a title="web design ireland" href="http://www.eire-webdesign.ie">Eire-Web Design Ireland</a></pre>
<p>This is the code for the file that will be called:</p>
<pre lang="php">$datax = $_POST['test'];
$datax1 = $_POST['test1'];
$datax2 = $_POST['test2'];
if($datax != "" || $datax1 != "" ){
echo "<strong>data posted: </strong>" . ($datax != "" ? "<strong>first input</strong> = ".$datax : "").
($datax1 != "" ? "<strong>second input</strong> = ".$datax1 : "").
($datax2 != "" ? "<strong>checkbox input</strong> = ".$datax2 : "<strong>checkbox input</strong> = unchecked")."";
exit();
}       

$data = "1";
if(isset($_GET['data']) &amp;&amp; $_GET['data'] != ""){
$data = $_GET['data'];
}
if($data =="1"){
?&gt;
<fieldset>
<legend> <strong>Welcome to Web Design Dublin Ireland  - Eire-WebDesign.ie</strong>  </legend>

	This is the second link
	</fieldset>

<!--p <-->}elseif($data == "2"){
echo "This is the second data that has been sent using GET.";
}elseif($data == "3"){ $datax = htmlspecialchars($_GET['test']);
echo "data posted: " . $datax;   

}
?&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eire-webdesign.ie/blog/2007/10/15/ajax-load-content-from-another-file-without-browser-refresh-using-ajax/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

