Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-26-2012, 05:31 PM
PM User |
#1
New Coder
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Submitting form with ajax?
Hey, I use this code:
Code:
<li><a href="#" id="jobs"></a>
<ul>
<li><a href="javascript :ajaxpage('/staffpanel/_frontend/jobApps.php', 'content_container');">Open Jobs</a></li>
</ul>
</li>
to display a job application form on my site. When they click "Open jobs" the job apps page loads into the content container. When I click submit on the form, it simply refreshes the page. My question is: How do I submit the form so that it doesn't just refresh the page?
Thanks
~ David
01-27-2012, 05:59 AM
PM User |
#2
Supreme Master coder!
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
Where's the definition of function ajaxpage() ?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
01-27-2012, 03:51 PM
PM User |
#3
New Coder
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
abduraooft
Where's the definition of function ajaxpage() ?
This is in my head tags if this is what you mean:
Code:
<script type="text/javascript">
/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""
function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}
function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}
function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}
</script>
Thanks
~ David
01-29-2012, 11:06 PM
PM User |
#4
New Coder
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
-bump- any1 know if this is possible? Thanks
~ David
02-01-2012, 05:19 PM
PM User |
#5
New Coder
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Sorry for bump again, anyone know how this can be done? I can provide the ajax page's code if that would help?
Cheers,
~ David
02-04-2012, 12:22 AM
PM User |
#6
New Coder
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Hey guys, been searching for a while now and maybe ajaxform() , not sure if this would work or even how to implement it. Any ideas?
Thanks guys
~ David
02-05-2012, 04:08 PM
PM User |
#8
Regular Coder
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
not ajax but this is how to
post a form
Code:
document.forms[0].onsubmit = function () {
var a = this.elements;
var b = "";
for (var i = a.length; i--; ) if (a[i].name) b += a[i].name + "=" + encodeURIComponent(a[i].value) + "&";
var req = new XMLHttpRequest;
req.open("POST", "whatever.php", false);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", b.length);
req.setRequestHeader("Connection", "close");
req.send(b);
elDiv.innerHTML = req.responseText;
return false;
};
Last edited by DaveyErwin; 02-05-2012 at 04:13 PM ..
02-06-2012, 08:35 PM
PM User |
#9
New Coder
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
defencedog
Quote:
Originally Posted by
DaveyErwin
not ajax but this is how to
post a form
Code:
document.forms[0].onsubmit = function () {
var a = this.elements;
var b = "";
for (var i = a.length; i--; ) if (a[i].name) b += a[i].name + "=" + encodeURIComponent(a[i].value) + "&";
var req = new XMLHttpRequest;
req.open("POST", "whatever.php", false);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", b.length);
req.setRequestHeader("Connection", "close");
req.send(b);
elDiv.innerHTML = req.responseText;
return false;
};
The way I got round it was use ajax to load the contents of the page, but have an iframe in a seperate document.
~ David
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 04:54 AM .
Advertisement
Log in to turn off these ads.