Jump to content

responseText always empty


UCCCC

Recommended Posts

Looking for some help here.  I cannot figure out why my responseText is always empty, even when the PHP file being accessed is a simple echo "Hello World";  Can anyone help?

 


var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
}

http.abort();
http.open("GET", "fbvalidate.php?name=" + user + "&eml=" + email + "&ref=" + referal, true);
http.onreadystatechange=function()
{
  if(http.readyState == 4)
  {
    if(http.responseText!="")
    {
	alert(http.responseText);
	return false;
    }
    elseif(pos !=1 )
    {
	http.send(null);
	return true;
            }
  }
}
http.send(null);

Link to comment
Share on other sites

Okay.  Thanks for the reply. I removed that line but still an empty response.  Just to cover my basis, my fbvalidate.php just looks like this

 

<?php

echo "Hello World";

?>

 

I know I kind of said that, but wanted to be specific incase there is a problem with the way I'm doing the fbvalidate.php file, although I would be surprised given its current simplicity.

Link to comment
Share on other sites

your ajax request is a little backwards, here is a skeleton that you can use as a guideline for your ajax request.

 

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

 

whenever i see someone using the javascript way of making an ajax request, i always recommend using the jquery ajax API

Link to comment
Share on other sites

Okay so I've been playing around and the problem I have apparently centers around the validation aspect itself.  The test code from AyKay47 worked fine until I tried to apply it to my form validation.  Of course the code is fine, but it did highlight the problem. Although I'm still not sure the solution.  If I add return false; to the end of my function, the responseText works fine.  However, if I try and do it any other way, it stops working.  I dont want the function to always return false. It should only return false if the responseText comes back with a string. Yet anytime I try and return True, it always turns True.  Here is where I am at now:

 



<html>
<head>
<script type="text/javascript">

function loadXMLDoc()
{

var xmlhttp;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   	alert(xmlhttp.responseText);
return false;
    }
  else
    {
     return true;
    }
  }
xmlhttp.open("GET","ajax_info.php?name=Roger&eml=emailaddress@gmail.com&ref=filler",true);
xmlhttp.send();

}

</script>
</head>
<body>


<form action='fbauthorize.php' method='post' name='fbform' onsubmit="return loadXMLDoc();">
			<center><big><big><b>Please Enter An Account Name:</big><br>
			<input type='text' name='username' id='username'><br><br>
			<input type='submit' value='Create Account'></form>

</body>
</html>


Link to comment
Share on other sites

Okay so I've been playing around and the problem I have apparently centers around the validation aspect itself.  The test code from AyKay47 worked fine until I tried to apply it to my form validation.  Of course the code is fine, but it did highlight the problem. Although I'm still not sure the solution.  If I add return false; to the end of my function, the responseText works fine.  However, if I try and do it any other way, it stops working.  I dont want the function to always return false. It should only return false if the responseText comes back with a string. Yet anytime I try and return True, it always turns True.  Here is where I am at now:

 



<html>
<head>
<script type="text/javascript">

function loadXMLDoc()
{

var xmlhttp;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   	alert(xmlhttp.responseText);
return false;
    }
  else
    {
     return true;
    }
  }
xmlhttp.open("GET","ajax_info.php?name=Roger&eml=emailaddress@gmail.com&ref=filler",true);
xmlhttp.send();

}

</script>
</head>
<body>


<form action='fbauthorize.php' method='post' name='fbform' onsubmit="return loadXMLDoc();">
			<center><big><big><b>Please Enter An Account Name:</big><br>
			<input type='text' name='username' id='username'><br><br>
			<input type='submit' value='Create Account'></form>

</body>
</html>


you'll have to explain the return logic a little better, im not quite following.

for the onsubmit event, simply loadXMLDoc(); is what you want, you don't need to return anything in the event caller, unless you plan on doing something with the return value.

glancing at the code, it looks to be correct, except normally you would want to return true if the responseText returned a value, false otherwise.

Link to comment
Share on other sites

So a user enters an account name. When they Submit, it pushes that account name, and some other information to the remote PHP file, currently ajax_info.php

 

The PHP file checks this information up against the database looking for matches. If there is a match, the account creation is stopped.  If there is no match, then the creation can proceed.

 

So, if there are no matches, the response output of ajax_info.php is empty.  If its not empty, then it's returning something like "This username is already in use, please select another," in which case the form submit should be stopped via the return false.

Link to comment
Share on other sites

So a user enters an account name. When they Submit, it pushes that account name, and some other information to the remote PHP file, currently ajax_info.php

 

The PHP file checks this information up against the database looking for matches. If there is a match, the account creation is stopped.  If there is no match, then the creation can proceed.

 

So, if there are no matches, the response output of ajax_info.php is empty.  If its not empty, then it's returning something like "This username is already in use, please select another," in which case the form submit should be stopped via the return false.

I would simply echo a notification regardless if a match was found or not, letting the user know whether or not there was a match and if they must enter another account name, returning false won't "stop submission", really in this context, returning false won't do much of anything.

Link to comment
Share on other sites

It blows my mind that this is so complicated.  There is no point in using Ajax to validate the form, if in the end, the form submit is going to happen either way making them use the Back button and re-load the page to try again. Isn't that the whole point of Ajax? Not having to re-load the page?

 

A Non Ajax javascript function, can be ran by an onsubmit, where return true allows the submit to take place, and a return false stops the form submit.  So I dont understand while adding in an ajax request (or whatever its technically called) suddenly cannot function the same way.

 

This code works fine and functions the way I've described (sans the ajax)

 

function loadXMLDoc()
{
var user = document.getElementById('username2').value;

if(user.length < 4)
{
	alert("That username is too short.");
	return false;
}
else
{
	return true;
}

}

 

So why cant the same thing be done with an ajax-style call rather than simply looking at a field length?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.