Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-17-2012, 01:34 PM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
listener not working

Im not sure whats up with the listener but the function is not working and i cant seem to find out whats wrong.

my other function works fine but when i paste this into the functions area (i dont even call it yet) then the other functions stop,
so it seems that maybe there is something broke here.

Code:

var xmlHttp
function tog_email(nemail,neid)
{
  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
  {
   alert ("Your browser does not support AJAX!");
   return;
  }
var url="email_availability.php";
url=url+"?nemail="+nemail"&neid="+neid;

xmlHttp.onreadystatechange=Changed;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}//close function toggle email

function Changed()
{
  if (xmlHttp.readyState==4)
  {
   settextflag();   
  }

}//close function changed



function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}//end function GetXmlHttpObject
and then the settextflag function is here (this is just for testing to make sure im finishing the process)

Code:
function settextflag(){
	

document.getElementById('emailswitch').value="p";


}
and then here are my inputs


Code:
<input type="text" id="hiddenemail" name="hiddenemail" value="<?php print("$email"); ?>" onchange="tog_email(this.value,$advuser);" />

<input type="text" id="emailswitch" name="emailswitch" />

a couple of points, the test for email_availablilty works fine, i tested it with direct url and with url vars and that file works fine.

im not an ajax person so i dont understand why im not getting the silent process being executed. When i remove the tog_email function and the rest of the on change httprequest stuff and call the settextflag() direct, then it works, when i change the field value for hiddenemail then the other input changes to a "p" but then when i add the tog_email and other httprequest stuff back (and not even call it) nothing works.

Last edited by durangod; 09-17-2012 at 06:09 PM..
durangod is offline   Reply With Quote
Old 09-17-2012, 01:40 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
url+"?nemail="+nemail"&neid="+neid;
should be
url+"?nemail="+nemail+"&neid="+neid;
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
durangod (09-17-2012)
Old 09-17-2012, 02:28 PM   PM User | #3
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Thanks so much, i was just looking at that this morning and nice catch, its always good to have other eyes look sometime. Thanks
durangod is offline   Reply With Quote
Old 09-17-2012, 04:50 PM   PM User | #4
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
i thought this was resolved but its not. its not grabbing the echo display and giving me a 200 or a readystate 4 and everything else works, the php display correctly,

i can do a test with just this in the function and it works fine

Code:
document.getElementById('emailswitch').value="p";
I did fix the issue before with the + and i did consolidate the function a bit

here is the new function, i decided not to pass the vars from the onchange but just to load them in the function.

Code:
function checkemail()
{
   //set var and check for browser support

  var xmlhttp;
  xmlhttp=GetXmlHttpObject();

  if (xmlhttp==null)
   {
   alert ("Your browser does not support AJAX!");
   return;
    }

//set all needed vars to run function

var nemail = document.getElementById('hiddenemail');
var neid = document.getElementById('hiddenuserid');
var url="email_availability.php";
url=url+"?nemail="+nemail+"&neid="+neid;

//now check which command to use per browser

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)
    {   

    //this is just a test to see if this executes

    document.getElementById('emailswitch').value="p";
    
    }else{

          // only test to see if executes defaut

          document.getElementById('emailswitch').value="no";


         // this will be the actual code when test passes 
         // grab the text that is echoed on the php process completion
         // display that text in the div called emailstatus

          //document.getElementById('emailstatus').innerHTML=xmlhttp.responseText;

          }//end else

 }//end onreadystatechange
 
// execute it quietly

xmlhttp.open("GET",url,true);
xmlhttp.send(null);

// just a test default trying to get something in return to problem solve
// this will be removed. 
document.getElementById('emailswitch').value="no";


}//close function checkemail
Again and ran the php file with doing the url manually in the browser and it works fine, passes all the echo tests.
The only thing i can think of is maybe the url is messed up but i dont know how to echo the url var so i can see it when i execute the function.

Last edited by durangod; 09-17-2012 at 04:54 PM..
durangod is offline   Reply With Quote
Old 09-17-2012, 05:01 PM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
You should encode the email and id before attaching them to the url:

Code:
var nemail = document.getElementById('hiddenemail');
nemail = encodeURIComponent(nemail);
var neid = document.getElementById('hiddenuserid');
neid = encodeURIComponent(neid);
var url="email_availability.php";
url=url+"?nemail="+nemail+"&neid="+neid;
I assume that you have a function named GetXmlHttpObject() although it doesn't seem that it is necessary - it's likely to be repeating some of the code that you already use.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 09-17-2012 at 05:04 PM..
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
durangod (09-17-2012)
Old 09-17-2012, 05:27 PM   PM User | #6
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
yeah that function was left over from the old code that split up the request into different functions. removed it and I am getting feedback now.

do i even need to set it to null and then check it, im guessing if i do then i need to do it after it checks the dif browser commands right.

Code:
if (xmlhttp==null)
   {
   alert ("Your browser does not support AJAX!");
   return;
    }
durangod is offline   Reply With Quote
Old 09-17-2012, 05:36 PM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
You could try this:

Code:
function getAjax() {
    var ajax = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        ajax = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE <= 8
        try {
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                ajax = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
    }
    return ajax;
}

function checkemail()
{
   //set var and check for browser support

  var xmlhttp;
  xmlhttp = getAjax();

  if (xmlhttp == false)
   {
   alert ("Your browser does not support AJAX!");
   return;
    }

//set all needed vars to run function

var nemail = document.getElementById('hiddenemail');
nemail = encodeURIComponent(nemail);
var neid = document.getElementById('hiddenuserid');
neid = encodeURIComponent(neid);
var url="email_availability.php";
url=url+"?nemail="+nemail+"&neid="+neid;

xmlhttp.onreadystatechange=function()
  {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {   

    //this is just a test to see if this executes

    document.getElementById('emailswitch').value="p";
    
    }else{

          // only test to see if executes defaut

          document.getElementById('emailswitch').value="no";


         // this will be the actual code when test passes 
         // grab the text that is echoed on the php process completion
         // display that text in the div called emailstatus

          //document.getElementById('emailstatus').innerHTML=xmlhttp.responseText;

          }//end else

 }//end onreadystatechange
 
// execute it quietly

xmlhttp.open("GET",url,true);
xmlhttp.send(null);

// just a test default trying to get something in return to problem solve
// this will be removed. 
document.getElementById('emailswitch').value="no";


}//close function checkemail
I assume that 'emailswitch' is an input element - so has a 'value' property.

Added: great page here!
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 09-17-2012 at 05:45 PM..
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
durangod (09-17-2012)
Old 09-17-2012, 05:44 PM   PM User | #8
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Previously I would receive a status of 0 (rather than 200) when testing locally. This no longer seems to be the case but it might be worth changing it from 200 to 0 temporarily just to exclude this possibility.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 09-17-2012, 05:45 PM   PM User | #9
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
yeah its an input but had value="" but i took off the value property because i figured it would be set by the function only since thats the only way i want to set that

I used it for testing to make sure when i was getting execution or not. Since i have the div working now i dont know if i still need that, so i might remove that or comment it out for testing later if i need it.

Thats great thanks, i had seen the old code before in another file but didnt know what it was or how it worked, this has been a learning experience and thats a good thing, now i know another way to do things and i like learning, i just wish i would not stay up all night doing so lol...

thanks sooooooooooooo much for helping me learn...

yeah the old code did not even have 200 in there it just checked for readystatus 4 is that ok?
durangod is offline   Reply With Quote
Old 09-17-2012, 05:45 PM   PM User | #10
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Code:
 
function getAjax() {
    var ajax = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        ajax = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE 6
        try {
IE7 has the window.XMLHttpRequest
DaveyErwin is offline   Reply With Quote
Old 09-17-2012, 05:51 PM   PM User | #11
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
yeah the old code did not even have 200 in there it just checked for readystatus 4 is that ok?
You should check both: 4 indicates the request completed, 200 indicates successful completion. You should read that article I linked

Ta @DaveyErwin
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 09-17-2012, 10:14 PM   PM User | #12
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Just wanted to follow up to say (for those that want value and not element) that you need to tweek that alittle and add .value attribute to get the value because it defaults to the element. All i did was did an alert until i got the value i wanted, thats the easiest way i think. I am not that far along after that i think i will have to prob use a diff output also to the div but not sure im not there yet.
durangod is offline   Reply With Quote
Old 09-18-2012, 07:10 AM   PM User | #13
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Hi another follow up, i did finally get it working after several hours of tweeking.
Now that i know how to test and work the process a bit, next time will go faster.
Today was a learning curve.

Anyway i wanted to let you know that not to use the else on the status values. This is what im talking about.

Code:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {  
        // grab the text that is echoed on the php process completion
         // display that text in the div called emailstatus


     document.getElementById('emailstatus').innerHTML=xmlhttp.responseText;
   
    }else{

             // this is the area im talking about 

            // alert('There was a problem with the request.');
         

          }//end else
You can leave the else there if you want just comment out the contents of it.
The reason i found is that it will keep popping up that message several times until it finishes and gets the right status code.
So if you just comment it out, you will just see the final result on the page and not have to sit there and keep clicking to close the multiple alerts.
durangod is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:41 AM.


Advertisement
Log in to turn off these ads.