2

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.

Community
  • 1
  • 1
fictimaph
  • 106
  • 6
  • try to give method = "GET/POST" in your form. that is
    – Alive to die - Anant Mar 20 '15 at 14:27
  • This may be helpful http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false – Radio Mar 20 '15 at 14:32
  • Why do you "test" the connection at all? Why don't you just submit your form and handle timeouts or non-200 responses? Even if your "test" tells you that connection is available, your actual form submit can fail. Therefore, the connection "test" is useless. – N.B. Mar 20 '15 at 16:09
  • I am having an issue on intranet connection with proxy error (that comes back around every 30min randomly). So I have to check the connection (using AJAX) but prefers to store the values with PHP, as Javascript is a client language. @Radio : I am looking at the subject of the propagation – fictimaph Mar 20 '15 at 16:18
  • 1
    But if you use one request as confirmation something is alive, it doesn't mean that the next request will also come through. The two requests share nothing in common that guarantees that if one succeeds, the other will too. But, if you prefer to spend time on completely useless feature - it's your time, your code and your project. I just wonder what conclusion you'll arrive at when you see your test connection succeeding and your next request failing. – N.B. Mar 21 '15 at 09:00

1 Answers1

1

Problems:
1. The TestConnection_js function makes a GET request, so you will not have any data in print_r($_POST);
2. The ajax request was made to index.php instead of aim.php (xhr.open('GET', './index.php');)
3. No data was sent by ajax request, so even the print_r($_GET); will have no data
4. If you want to sent POST request, which I assume is that what you want, you have to serialize the form and make a POST ajax request to aim.php file

Solution:
1. I recommend you to use a javascript framework (like jQuery) that does most of the code you want and the code is cross-browser compatible. Take a look at jQuery ajax POST requests.
Also, take a look at this example of jQuery POST request with the form data.
2. Use the developer tool integrated in your browser. My preferred tool is Firebug for Firefox. This way you can see exactly each request the browser is making and the data is being sent/received.

Community
  • 1
  • 1
machineaddict
  • 3,216
  • 8
  • 37
  • 61
  • Well Maybe I wasn't clear. I try to test the connection to my site (on the main page) before sending my Form on the specific page the Form has to go. – fictimaph Mar 20 '15 at 15:36
  • That was one of the things I was talking about. The other thing was the form submittion after the ajax request. – machineaddict Mar 23 '15 at 07:33
  • I see the point you and @N.B. are making here. The use of jQuery makes the fiability of AJAX worth it (and so my script useless). The only difficulty I wanted to avoid was to learn another language as I am learning by myself and it will takes time. I'm on it. Thanks all for the Help, I will post here the jQuery solution (witch is trivial I'm sure) as soon as possible. – fictimaph Mar 23 '15 at 11:20