0

Can anybody help or guide me into finding out why the following code will not operate as expected (i.e alert the echo from php) and instead alerts the "something negative happened"?

My Javascript code as follows:

window.onload = function(){

    var regForm = document.getElementById("register").onsubmit = checkForm;
}

//Let's see if this user already exists

function checkForm(){   

alert("You pressed Register!");

var cc = document.getElementById("un3").value;

    if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
        alert("You are using XML HTTP REQUEST");
    }else{
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        alert("YOU ARE USING ACTIVE X OBJECT");
    }

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
                alert(xmlhttp.responseText);
            return false;
            }else{
            alert("Something negative happened");
        }
    }

    xmlhttp.open("GET","user_exists.php?q="+cc,true);
    xmlhttp.send();
}

My php file simplified to get this thing to work:

<?php

$q=$_GET["q"];

echo "something good happened";

?>
Tez
  • 75
  • 8

2 Answers2

0

That's because onreadystatechange also changes when connecting before the result is there.

Try this:

xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4) {
        // only check when state is "the communication is done"
        if (&& xmlhttp.status==200){
             alert(changes when connecting before the result is therexmlhttp.responseText);
             return false;
        }else{
              alert("Something negative happened");
     } else {
            // ignore other states
     }
}

Better though, is to use a decent JavaScript library like jQuery or dojo so you don't have to do all this yourself.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Yes I have a feeling that the connection is happening before the result is there. Unfortunately I have tried your suggestion, and it is still not working. :( – Tez Jan 11 '14 at 17:04
  • Try logging the response state to the Javascript console, so you see it actually goes through all the steps. – Bart Friederichs Jan 11 '14 at 17:11
  • Javascript is not showing any errors, when I alert the xmlhttp.response, it shows that $q in my php file has no index/not defined. Not sure why this keeps happening. I've checked my php file on it's own, and it works, but combined with javascript; nothing. – Tez Jan 12 '14 at 13:25
  • To clarify further, if I eliminate $q then what is echoed/alerted is "something good happened". The problem appears to be receiving the $q from my javascript, then sending this into mysql data to get the results. Needless to say, without the javascript, the php file works on its own. – Tez Jan 12 '14 at 13:35
  • @Tez is the value `cc` there? Also, you can use Firebug to see what's going on "under the hood". – Bart Friederichs Jan 13 '14 at 07:27
0

Try this:

xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4){
        if(xmlhttp.status==200){
            alert(xmlhttp.responseText);
            return false;
        }else{
            alert("Something negative happened");
        }
     }
 }

readyState comes by several times, not always with 4. Take a look at this question

I prefer to take the response out of the request, to keep my code clean. In your case that would be:

xmlhttp.onreadystatechange=responshandler;

function responsehandler(){
   //here the response code
}

make sure tough you declare var xmlhttp; outside the function to make it global.

Community
  • 1
  • 1
Michel
  • 4,076
  • 4
  • 34
  • 52
  • Thanks for your endeavours, but it's still not working for me. – Tez Jan 11 '14 at 17:04
  • put `alert(xmlhttp.readyState)` before `if(xmlhttp.readyState==4)`. Does it reach 4? – Michel Jan 11 '14 at 17:23
  • No it does not reach 4 and yes user_exists is correct. I'm baffled because I have another piece of code doing something similar which works and uses the same principle. I can only assume that it has something to do with the form, or that I'm doing something really stupid that I haven't yet noticed. – Tez Jan 11 '14 at 19:03
  • Ah. HAd that before. Check if all your div's are closed inside the form. Otherwise it won't post. – Michel Jan 12 '14 at 07:36
  • The problem is that $q is not being defined on my php file. Therefore for some reason, my javascript is not connecting to send information to the php file to define $q (I think)...and I have no idea how to make this thing work! haha – Tez Jan 12 '14 at 13:26
  • Try this: xmlhttp.open("GET","user_exists.php",true); xmlhttp.send('q='+cc);. Maybe that works – Michel Jan 13 '14 at 08:20