Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ajax Newbie

Status
Not open for further replies.

PBE3

Programmer
Aug 16, 2007
25
US
Hello all - I am trying to do what I thought would be simple Ajax function. I am trying to compare what is in a text box to what is retrieved from a mysql table, but I can not get the js to read the response given from my php page. It is blank. Here is my code. Thanks ahead of time.

***JS***
function fields(){
email=document.add.email.value;
if (email!=''){
alert('init!');
var httpObject=getHttpObject();
if (httpObject==null){
alert ("Your browser does not support AJAX!");
return false;
}
var url="/map/ppmap/users/getEmail.php";
url=url+"?e="+email;
alert(url); //this is working
httpObject.onreadystatechange=function(){state_Change(httpObject);}
httpObject.open("GET",url,true);
httpObject.send(null);

}
}

function state_Change(httpObject){
if (httpObject.readyState==4){
alert(httpObject.responseText);//no response
if(httpObject.responseText==0){
alert('1'+httpObject.responseText);//no response
return true;
}else{
alert('2'+httpObject.responseText);
document.getElementById('emailAdd').innerHTML = httpObject.responseText;
return false;
}
}
}
function getHttpObject(){
if (window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}else if (window.XMLHttpRequest){
return new XMLHttpRequest();
}else {
alert("Your browser does not support AJAX.");
return null;
}
}

***PHP***
$query="SELECT email FROM bw_users_users";

$eAdd = $gBitUser->pp_exec_sql($query);
$eArray=array();
$e=$_GET['e'];

for($j=0; $j<count($eAdd); $j++){
foreach(array_keys($eAdd[$j]) as $myKey){
array_push($eArray, $eAdd[$j][$myKey]);
}

if ($e==$eAdd[$j])
{
echo "Email address already exists. Please enter a different email address";
}else{
echo "0";
}
}
 
Do you know that your PHP is working (i.e. if you visit the URL manually, do you see a response?)

Also, are your files being run from a server ( or localal file system (file://)? If the latter, you'd need to put them on a web server.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks for the reply! I do get a response, sorry forgot to mention that, and its running on a server.

 
I can't get the response from the php page.
 
My php if fine. Going to the url I get a response, it's the ajax I am not too keen with. I am writing 'echo' in the php, but I don't think I am grabbing it correctly from the ajax function. When I alert(httpObject.responseText) it is blank.
 
No I get a response when I go to the php page that I am calling, but in the javascript httpObject.responseText is empty.

I do not understand why.
 
Thanks alot! I appreciate your help.
I've discovered something weird that is happening and maybe Ajax experts can help me solve my problem. My Ajax works when I use is soley, but as I integrated in my applications it stops working. In my script I have added an alert if one of my select boxes are empty, if it is then my ajax works and I get my correct error, but if the select is not empty and the email is still wrong my ajax does not work?

function fields(){
val=document.add['saved[]'].length;
if (email!=''){
alert('init!');
getHttpObject();
}

//if this is true ajax spits out correct error else it ignores the ajax function-->
if(val>0){
for(i=0; i<val; i++){
document.add['saved[]'].options.selected=true; }
} else if(val==0){
alert('No stores selected. Please select users stores for viewing.');
return false;
}
return true;
}

function state_Change(httpObject){
if (httpObject.readyState==4){
// var response= httpObject.responseText;
alert(httpObject.responseText);
if(httpObject.responseText == 0){
//doesn't exist
}else{
// document.add.getElementById('emailAdd').innerHTML = httpObject.responseText;
alert(httpObject.responseText);
Highlight(document.add.email,'red');
return false;
}
}
}

function getHttpObject(){
email=document.add.email.value;
if (window.ActiveXObject){
httpObject = new ActiveXObject("Microsoft.XMLHTTP");
}else if (window.XMLHttpRequest){
httpObject = new XMLHttpRequest();
}else {
alert("Your browser does not support AJAX.");
return null;
}

var url="getEmail.php";
url=url+"?e="+email;
alert(url);
httpObject.open("GET",url,true);
httpObject.onreadystatechange=function(){state_Change(httpObject);}
httpObject.send(null);
}
 
I have tried that as well. The only way it will give me a response to the email existing or not, is when val==0

It just doesn't make sense. If I val>0 it doesn't give me a response.

:-(

Thanks again for helping.
 
It's currently on my server's ip, it wouldn't be able to connect via browser. :-(


On submit it goes to the function.

<form name="add" legend="Add a new user" method="GET" action="/map/users/pp_addUser.php" onSubmit="return fields();" >

 
I'm thinking that what might be happening is that if val isn't 0, then you are returning "true", and your form is being submitted. Thus your AJAX will never have anywhere to call back (as the page is being reloaded).

If you return "false" when val isn't 0 as well as when it is, does that fix things?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
If I do that then when val isn't 0 it won't submit.
My I feel like I have tried everything. I'm so confused.
 
I'm out of ideas, and without having a URL to the actual code you have (after all, you've said it works until you integrate it with your app), I'll have to step out of this and see if someone else can help.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top