Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    im=nput using xmlHttp

    I am a web novice and I have a site I inherited. One page is an online chart displaying volunteer names. People can input their name, edit or delete them. For some reason the input is not working. Nothing changed on the page. The problem is when editing the names on the page there is a popup error stating "failed to save" What is my next move here, I am struggling to understand this java function and why it's not working. Here is some of the script. I don't really understand it, but I know this is controlling the input:

    Code:
     <script type="text/javascript">
    	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;
    			}
    			
    			function stateChanged() 
    			{ 
    	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    				{ 
    	var id = xmlHttp.responseText;
    					if (id > 0)
    					{						
    	var input = document.getElementById("input"+id);
    	var obj = document.getElementById("span"+id);
    	obj.innerHTML = input.value;						
    	anchor = document.getElementById("anchor"+id);									anchor.innerHTML = "edit";
    	anchor.setAttribute('onclick', 'javascript:editName('+id+')'); 
    					}
    					else
    					{
    						alert('failed to save');
    					}
    				} 
    			}
    			function editName(id)
    			{					
    		var obj = document.getElementById("span"+id);					
    		obj.innerHTML = '<input size="45" type="text" id="input'+id+'" value="'+obj.innerHTML+'"/>';
    		anchor = document.getElementById("anchor"+id);				
    		anchor.innerHTML = "save";
    		anchor.onclick = function() { saveName(id)}; 								
    			}
    			
    			function saveName(id)
    			{			
    		xmlHttp=GetXmlHttpObject();
    				if (xmlHttp==null)
    				{
    		alert ("Browser does not support HTTP Request");
    					return;
    				}
    		var url="ajax.php";
    		url=url+"?id="+id;
    		var input = document.getElementById("input"+id);
    				url=url+"&name="+input.value;				
    				xmlHttp.onreadystatechange=stateChanged;
    				xmlHttp.open("GET",url,true);
    				xmlHttp.send(null);
    			}
    		</script>
    Last edited by VIPStephan; 06-27-2013 at 08:28 AM. Reason: added code BB tags

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    change

    Code:
    alert('failed to save');
    to

    Code:
    alert('failed to save. Response text : '+id);
    and tell us what the new alert says

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts
    I am not a js or ajax expert but you wanted to know what this was, its ajax. Basically what ajax does is allows you to set up another page process many times its a php page with output to the user, in your case its a file called ajax.php and this process grabs that output and uses it on this page.


    For example you have a php file called sample.php and all it does is display the text hello back to the user. Well what you can do is you can in simple terms listen for that output by telling ajax in this file that you want to grab the output from that sample.php file when it is done executing.

    The stateChanged is what ajax watches for, when that is status 4 or completed (i think status 2 works as well) then the http object basically is the word "hello" and ajax sees by the status that the file is done executing and so it grabs the value of that "hello" text using the function saveName.

    As far as all the other stuff the document.getElementById its grabbing the value from that id value and basically your building a final value with this process. Grabbing some data here some there.

    So i hope that basically explains whats going on, im not an expert so i cant tell you why exactly its not working and i dont know what you mean by not working, are you getting an error or is there no output or what. But if you explain what you need that is not happening or maybe even give a screen shot somone can prob help you. But also they can prob just look at the code and see if they see any errors.

    Hope that helps.


    How did i do DanInMa?
    Last edited by durangod; 06-27-2013 at 08:21 AM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,518
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    I don't think DanInMa's change will help find the problem.

    True, it will show that what is coming back from the AJAX call is not a number greater than zero, but the real root of the problem is almost surely in the PHP code that is sending back that bad number.

    I think the more important thing is to find out what the URL is that is being used when the error occurs.

    So to that end, I'd add a debug line here:
    Code:
    		url=url+"?id="+id;
    		alert("Calling AJAX with url: " + url);
    Then, when the failure occurs, open up another browser window or tab and hit that same URL directly from the browser, so you can see what the PHP code is doing (it will, presumably, display a number <= 0, and possibly error messages). And then start debugging the PHP code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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