Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #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

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Where's the definition of function ajaxpage()?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    86
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #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

  • #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

  • #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

  • #7
    New Coder
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    86
    Thanks
    20
    Thanked 0 Times in 0 Posts
    hey I have a similar problem but quiet imple can anyone help:
    Submit form with no page change

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 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.

  • #9
    New Coder
    Join Date
    Dec 2011
    Posts
    86
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by defencedog View Post
    hey I have a similar problem but quiet imple can anyone help:
    Submit form with no page change
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •