Ajax Request Results in form being submitted aswell

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

As the title states my Ajax call is actually causing the form to be submitted to its default action, why? I ve never come across this before, all my other ajax calls have never done this.

function newAjax(t,u){ //t = type(post/get), u = url
    var resource = null;
    if (window.ActiveXObject) { 
        resource = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    else if (window.XMLHttpRequest) { 
        resource = new XMLHttpRequest(); 
        resource.overrideMimeType( text/html );
    }
    resource.open(t,u,false);
    resource.setRequestHeader( Content-Type ,  application/x-www-form-urlencoded; charset=UTF-8 );
    return resource;
}

function send_newsletter(){
    formObj = document.getElementById("news_form");
    var inputs = formObj.getElementsByTagName("INPUT");
    var parameters = "";
    for(i = 0; i < inputs.length; i++){
        parameters += inputs[i].name+"="+encodeURI(inputs[i].value);
        if(i != inputs.length-1){
            parameters += "&";
        }
    }
    var url = "whereitshouldbegoing.com";
    var ajax = newAjax("POST",url);
    ajax.onreadystatechange = ajaxResult;
    ajax.setRequestHeader("Content-length", parameters.length);
    ajax.setRequestHeader("Connection", "close");
    ajax.send(parameters);
}

It all works fine upto the .send line, which is the bugger which is causing the form to submit aswell(I also have no idea if the ajax request actually gets off).

The send_newsletter function is called from an input type="image" element with onclick="send_newsletter()"

Please don t tell me to use jQuery or another library, as much as I would love to, we can t use any external librarys, corporate guidelines and whatnot.

Answers

http://www.w3.org/TR/html401/interact/forms.html#h-17.4.1 http://www.w3.org/TR/html401/interact/forms.html#h-17.4.1

<input type="button" onclick="send_newsletter()" value="Send" />

<button onlclick="send_newsletter()">... </button>

#
#

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/1644870/ajax-request-results-in-form-being-submitted-aswell

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils