DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Variable scope from XMLHttpRequest Callback

  1. #1
    Join Date
    Aug 2006

    Unhappy Variable scope from XMLHttpRequest Callback

    Dear All

    I am trying to implement and XMLHttpRequest call for form validation.

    I have the code working where it checks to see if an email address supplied already exists. This works, and so does the form validation, however the problem lies within a variable scope issue. The global variable errors is used to let the user know what errors there are with the form. I have declared the variable as a global variable and yet when I set it to nothing from within the XMLHttpRequest function call it treats it as a local variable. If I don't set it to nothing the function works apart from an ever increasing error string.

    Not really sure where I am going wrong so any comments are very welcome.

    Here is the code for the XMLHttpRequest and form validation function.

    var request = false;
    var smt = false;
    // Initiate the XMLHTTP Request object
    try {
    	request = new XMLHttpRequest();
    	//alert("XMLHttpRequest Created");
    } catch (trymicrosoft) {
    	try {
    		request = new ActiveXObject("Msxml2.XMLHTTP");
    		//alert("Msxml2.XMLHTTP created");
    	} catch (othermicrosoft) {
    		try {
    			request = new ActiveXObject("Microsoft.XMLHTTP");
    			//alert("Microsoft.XMLHTTP created");
    			} catch(failed) {
    				request = false;
    // alert if not initialised	
    if (!request)
    	alert("Error initialising XMLHttpRequest!");
    // email checking function	
    function getEmail() {
    var e = document.form1.Email.value;
    var url="checkemail.asp?q="+escape(e);"GET", url, true);
    request.onreadystatechange = updatepage;
    function updatepage() {
    if (request.readyState ==4)
    	if (request.status ==200) {
    		if (request.responseText!=0) {
    		errors+="Email already exists\n";
    	} else {
    	alert ("Error: "+ request.status);
    function check() {
    var errors = "";
    //alert("Error report from check() function after email check call\n:"+errors);
    var f = document.form1;
    var em = f.Email.value.indexOf('@');
    if (!f.Email.value) {
    	errors+='Email rqd\n';
    	} else {
    	if (em==-1) {
    	errors += 'Correct Email Address\n';
    if (!f.Firstname.value) { errors+='Firstname rqd\n'; }
    if (!f.Surname.value) {errors += 'Surname rqd\n';}
    if (!f.Address1.value) {errors+='Address Line rqd\n';}
    if (!f.City.value) {errors+='City rqd\n';}
    if (!f.County.value) {errors+='County rqd\n';}
    if (!f.Postcode.value) {errors+='Postcode rqd\n';}
    if (!f.Country.value) {errors+='Country rqd\n';}
    if (errors) {
    return false;
    } else {
    return true;
    The other point is that when the Email form field is changed or exited the function is called.

    Many thanks

  2. #2
    Join Date
    Aug 2003
    Can you just move the declaration of errors into the global scope? e.g.
    var errors = "";
    function updatePage() {
      errors = "";
      if( ... ) {
        errors = errors + "bad email address";
      } else {
        errors = errors + "good email address";
    Hope this helps.

Similar Threads

  1. How to retain session variable
    By NikhilSriv in forum Java
    Replies: 1
    Last Post: 06-14-2006, 05:24 AM
  2. SQL Insert Trigger; variable scope issue
    By rkbnair in forum Database
    Replies: 1
    Last Post: 05-31-2006, 03:52 AM
  3. Syntax Shortcut Suggestion
    By Patrick Troughton in forum .NET
    Replies: 91
    Last Post: 04-12-2002, 06:50 PM
  4. sql variable problem
    By Lymarie in forum Database
    Replies: 0
    Last Post: 04-02-2001, 10:16 AM
  5. Replies: 0
    Last Post: 03-30-2001, 03:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center