I would like to test the connection before submit. Unlike that solution, I am using a non-Jquery solution (because I don't want to go into that language and I think it isn't necessary to load the librarie).
The code works fine in testing the connection and in sending or not the form.
The problem seems to be that using formTemp.submit()
in xhr.onreadystatechange
, send the form without the inputs.
Here is what I do :
//Test connection on form
var myForm = document.getElementsByTagName('form');
for (var i = 0 ; i <myForm.length; i++){
(function(){
var currentI = i;
addEvent(myForm[currentI],'submit', function(e) {
e = e || window.event; //In case IE
e.preventDefault(); //Prevent the Form to be sent
TestConnection_js(this);
});
})();
}
function addEvent(element, event, func){
if (element.addEventListener){
element.addEventListener(event, func, false);
} else { //In case IE
element.attachEvent('on'+event, func);
}
}
var TestConnection_js = function (formTemp){
var xhr = new XMLHttpRequest();
xhr.open('GET', './index.php');
xhr.onreadystatechange = function(){
console.log(xhr.readyState+' - '+xhr.status);
if (xhr.readyState == 4 && xhr.status == 200) {
formTemp.submit(); //Here is my Submit (that submit nothing)
} else {
alert('Connection Error');
}
};
xhr.send(null);
return xhr;
}
<!-- What's on page : here.php -->
<form action="aim.php" method="post">
<input type="submit" value="Aim" />
</form>
<!-- What's on page : aim.php -->
<?php print_r ($_GET);
print_r ($_POST); ?>
I guess that the fact it end up on aim.php means success test, so could you tell me what is wrong here ?
Edit : Forgot the method="post"
on the form, but that is not the problem.