CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2009
    Posts
    4

    Exclamation Seperate Div refresh in the same function.

    Okay so, I found the code below and adapted it so it could be used on multiple pages; login, registration, etc.

    What I'm making is a game, and on a certain page areas of the page will need to be reloaded. As soon as I added a new parameter (div) into the functions the alertContents doesn't seem to work.

    The functions work like this:
    In the html form code the get function collects the information on the form, then, depending on the page parameter, the switch statement collects the forms code for the post information. The post information is then passed into the post function where the function determines the browser that is being used, then set the function to run when the http request state is changed. This is the point where I believe it to be broken. From there the alertContents function outputs the result of the processing from the server.

    code:
    Code:
    var http_request = false;
    function makePOSTRequest(url, parameters, div) 
    {
    	http_request = false;
    	if (window.XMLHttpRequest) 
    	{ // Mozilla, Safari,...
    		http_request = new XMLHttpRequest();
    		if (http_request.overrideMimeType) 
    		{
    			// set type accordingly to anticipated content type
    			//http_request.overrideMimeType('text/xml');
    			http_request.overrideMimeType('text/html');
    		}
    	}
    	else if (window.ActiveXObject) 
    	{ // IE
    		try 
    		{
    			http_request = new ActiveXObject("Msxml2.XMLHTTP");
    		} 
    		catch (e) 
    		{
    			try 
    			{
    				http_request = new ActiveXObject("Microsoft.XMLHTTP");
    			} 
    			catch (e) 
    			{
    			}
    		}
    	}
    	if (!http_request) 
    	{
    		alert('Cannot create XMLHTTP instance');
    		return false;
    	}
    
    	http_request.onreadystatechange = alertContents(div);			
    	http_request.open('POST', url, true);
    	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	http_request.setRequestHeader("Content-length", parameters.length);
    	http_request.setRequestHeader("Connection", "close");
    	http_request.send(parameters);
    }
       
    function alertContents(div) 
    {
    	if (http_request.readyState == 4) 
    	{
    		if (http_request.status == 200) 
    		{
    			//alert(http_request.responseText);
    			result = http_request.responseText;
    			document.getElementById(div).innerHTML = result;                     
    		} 
    		else 
    		{
    			alert('There was a problem with the request.');
    		}
    	}
    }
       
    function get(obj, page, div) 
    {
    	switch (page)
    	{
    		case 'login.php':
    			var poststr = "un=" + escape(encodeURI( document.getElementById("un").value )) +
    							"&pw=" + escape(encodeURI( document.getElementById("pw").value )) +
    							"&login=" + escape(encodeURI( document.getElementById("login").value ));
    			makePOSTRequest(page, poststr, div);
    			refresh();
    		break;
    		case 'register.php':
    			var poststr = "un=" + escape(encodeURI( document.getElementById("un").value )) +
    							"&pw=" + escape(encodeURI( document.getElementById("pw").value )) +
    							"&cpw=" + escape(encodeURI( document.getElementById("cpw").value )) +
    							"&em=" + escape(encodeURI( document.getElementById("em").value )) +
    							"&register=" + escape(encodeURI( document.getElementById("register").value ));
    			makePOSTRequest(page, poststr, div);
    		break;
    		case 'chat_insert.php':
    			var poststr = "msg=" + escape(encodeURI( document.getElementById("msg").value ));
    			makePOSTRequest(page, poststr, div);
    		break;
    		case 'home.php':
    			var poststr = "logout=" + escape(encodeURI( document.getElementById("logout").value ));
    			makePOSTRequest(page, poststr, div);
    			refresh();
    		break;
    	}
    }

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Seperate Div refresh in the same function.

    Have you tried debugging it yet with Firefox's Error Console? I suggest you do that first.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Jan 2009
    Posts
    4

    Re: Seperate Div refresh in the same function.

    I don't use firefox, but i'll download it and give it a go. Thanks

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Seperate Div refresh in the same function.

    You should have Internet Explorer, Firefox, and Safari for testing at all times. Firefox's Error Console is a great debugging tool. Also, you should look into Firebug for debugging as well.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Seperate Div refresh in the same function.

    And if you want to make sure, you should have more than one machine to get a good overview of different versions of IE and to a lesser extent FF.
    I'd also just like to add that I prefer Chrome's debugger (but each to their own; it's mainly because I use my C-style include method for complex website scripts, and Chrome loads and runs it that bit quicker...)
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  6. #6
    Join Date
    Jan 2009
    Posts
    4

    Re: Seperate Div refresh in the same function.

    Yeah I use chrome, I used to use firefox before chrome came out.

    Where is chromes debugger?

  7. #7
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Seperate Div refresh in the same function.

    Right click -> Inspect Element. Somewhere on the bottom bar of that window, there's the console. It's useful, because if you open it, and hen reload the page (it's tied to the process from which you open it) it also stores a copy of all the resource files, so it'll show you on which line errors occur.
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  8. #8
    Join Date
    Jan 2009
    Posts
    4

    Re: Seperate Div refresh in the same function.

    Ahh yes, i've used the inspect element but didn't notice the console before. Thanks, i'll check it out

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured