Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-26-2012, 05:31 PM   PM User | #1
Vernier
New Coder

 
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Vernier is an unknown quantity at this point
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
Vernier is offline   Reply With Quote
Old 01-27-2012, 05:59 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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)
abduraooft is offline   Reply With Quote
Old 01-27-2012, 03:51 PM   PM User | #3
Vernier
New Coder

 
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Vernier is an unknown quantity at this point
Quote:
Originally Posted by abduraooft View Post
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
Vernier is offline   Reply With Quote
Old 01-29-2012, 11:06 PM   PM User | #4
Vernier
New Coder

 
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Vernier is an unknown quantity at this point
-bump- any1 know if this is possible? Thanks

~ David
Vernier is offline   Reply With Quote
Old 02-01-2012, 05:19 PM   PM User | #5
Vernier
New Coder

 
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Vernier is an unknown quantity at this point
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
Vernier is offline   Reply With Quote
Old 02-04-2012, 12:22 AM   PM User | #6
Vernier
New Coder

 
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Vernier is an unknown quantity at this point
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
Vernier is offline   Reply With Quote
Old 02-04-2012, 10:55 PM   PM User | #7
defencedog
New Coder

 
Join Date: Mar 2011
Location: Pakistan
Posts: 85
Thanks: 20
Thanked 0 Times in 0 Posts
defencedog has a little shameless behaviour in the past
hey I have a similar problem but quiet imple can anyone help:
http://www.codingforums.com/showthread.php?t=250621
defencedog is offline   Reply With Quote
Old 02-05-2012, 04:08 PM   PM User | #8
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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..
DaveyErwin is offline   Reply With Quote
Old 02-06-2012, 08:35 PM   PM User | #9
Vernier
New Coder

 
Join Date: Dec 2011
Posts: 86
Thanks: 1
Thanked 0 Times in 0 Posts
Vernier is an unknown quantity at this point
Quote:
Originally Posted by defencedog View Post
hey I have a similar problem but quiet imple can anyone help:
http://www.codingforums.com/showthread.php?t=250621
Quote:
Originally Posted by DaveyErwin View Post
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
Vernier is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, form, javascript, refresh, submit

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:08 PM.


Advertisement
Log in to turn off these ads.