Two ajax functions on one page? mine are fighting!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cbellew
    New Member
    • Sep 2007
    • 26

    Two ajax functions on one page? mine are fighting!

    Hi guys,

    Let me just introduce my problem - i hope someone will be able to help! I have an index page for my site where i want the user to have the choice to view a login form or registration form. I have index.php with two links ("Click to login", "Click to Register"). I want to use ajax so that the user can switch between the forms without having to load the page. At the moment i have the forms stored in seperate html pages and i want the revelvant page to fill itself into a div called "content", depending on what the user would like to see. Here is my code:

    Code:
    <html>
    <head>
    <script type="text/javascript" language="javascript">
    function registerRequest() {
    	var httpRequest;
    	var url="registration_form.php"
    
    	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    		httpRequest = new XMLHttpRequest();
    		if (httpRequest.overrideMimeType) {
    			httpRequest.overrideMimeType('text/xml');
    			// See note below about this line
    		}
    	} else if (window.ActiveXObject) { // IE
    		try {
    			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try {
    				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e) {
    			}
    		}
    	}
    
    	if (!httpRequest) {
    		alert('Your browser does not support the features needed for this website');
    		return false;
    	}
    	httpRequest.onreadystatechange = function() {
    		if (httpRequest.readyState == 4) {
    			if (httpRequest.status == 200) {
    				document.getElementById('content').innerHTML = httpRequest.responseText;
    			} else {
    				alert('There was a problem with the site.');
    			}
    		}
    	};
    	httpRequest.open('GET', url, true);
    	httpRequest.send('');
    	httpRequest.close;
    }
    function loginRequest() {
    	var httpRequest2;
    	var url2="login_form.php"
    
    	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    		httpRequest2 = new XMLHttpRequest();
    		if (httpRequest2.overrideMimeType) {
    			httpRequest2.overrideMimeType('text/xml');
    			// See note below about this line
    		}
    	} else if (window.ActiveXObject) { // IE
    		try {
    			httpRequest2 = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try {
    				httpRequest2 = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e) {
    			}
    		}
    	}
    
    	if (!httpRequest2) {
    		alert('Your browser does not support the features needed for this website');
    		return false;
    	}
    	httpRequest2.onreadystatechange = function() {
    		if (httpRequest2.readyState == 4) {
    			if (httpRequest2.status == 200) {
    				document.getElementById('content').innerHTML = httpRequest2.responseText;
    			} else {
    				alert('There was a problem with the site.');
    			}
    		}
    	};
    	httpRequest2.open('GET', url2, true);
    	httpRequest2.send('');
    }
    </script>
    </head>
    <link rel="stylesheet" type"text/css" href="css/style.css" />
    <body>
    <div id="choice">
    <table>
    	<tr>
    		<td><u><a onMouseUp="loginRequest()">Click to Login</a></u>
    		</td>
    	</tr>
    	<tr>
    		<td><u><a onMouseUp="registerRequest()">Click to Register</a></u>
    		</td>
    	</tr>
    </table>
    </div>
    <div id="cell">
    <div id="content">
    <?php include "login_form.php"; ?>
    </div>
    </div>
    </body>
    In firefox only the registration link is working (or so it seems). In IE it is the same, although it tells me that there is an object expected on line 48 ("httpRequest2. overrideMimeTyp e('text/xml')"). Is it ok to have two ajax function s on the same page, after all they shouldnt be both being used at the same time?

    Thanks for your time guys.

    Chris
  • rpnew
    New Member
    • Aug 2007
    • 189

    #2
    hi,
    There is nothing wrong with using two function on the same page.
    I've used more than that with my project so i dont think its a problem. Your problem is somewhere else as i think......

    Regards,
    RP
    Last edited by acoder; Jan 14 '08, 11:32 AM. Reason: removed quote

    Comment

    • cbellew
      New Member
      • Sep 2007
      • 26

      #3
      Thanks, i thought as much, i will keep trying to figure it out myself.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Why are you 'closing' the request on line 40?

        Comment

        Working...