Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Poltergeist in my code?

Status
Not open for further replies.

Tracey

Programmer
Oct 16, 2000
690
NZ
Could someone please please (before i tear my hair out [hairpull2]) tell me why the first code works but the second code errors with "object required" when there is only one line of code difference (for "start"), but the code is basically the same code that works for "name1"

Working
Code:
function testChoice(user){
		    $('progress').innerHTML = 'Processing request';
			var url = 'data.xml';
			var GroupID = $F('groupid');
			var id = $F('id');
			var pars = 'GroupID=' + GroupID + '&ID=' + id;
		
			var req = new Ajax.Request('test.xml',{method:'get',onComplete:processResult});
		}
		function processResult(response){
			try{
			
			var val = response.responseXML;
			
			//debugger;
			var objName = val.getElementsByTagName('name');
			
			var objDateStarted = val.getElementsByTagName('datestarted');
		
			$('name1').value = objName.item(0).text; 
				
			$('progress').innerHTML = '';
		alert("finished");
			}//try
			catch(e){
				alert(e.message);
			}
		}

Not working
Code:
function testChoice(user){
		    $('progress').innerHTML = 'Processing request';
			var url = 'data.xml';
			var GroupID = $F('groupid');
			var id = $F('id');
			var pars = 'GroupID=' + GroupID + '&ID=' + id;
		
			var req = new Ajax.Request('test.xml',{method:'get',onComplete:processResult});
		}
		function processResult(response){
			try{
			
			var val = response.responseXML;
			
			//debugger;
			var objName = val.getElementsByTagName('name');
			
			var objDateStarted = val.getElementsByTagName('datestarted');
		
			$('name1').value = objName.item(0).text; 
			//this line of code errors!!
			$('start').value = objDateStarted.item(0).text;
		
			$('progress').innerHTML = '';
		alert("finished");
			}//try
			catch(e){
				alert(e.message);
			}
		}

Below are the html of the page, and xml files referenced above

HTML
Code:
<body>
   
    <form name="groupform" id="groupform" method="post" form action="[URL unfurl="true"]http://mydomain/cgi-bin/myapp.exe/savesubscription"[/URL]
        onsubmit="return submitonce()">
        <span id="progress"></span>
        <input type="hidden" name="ID" value="29">
		<input type="hidden" name="groupid" value="55">
        <table bgcolor="#F4BAD8" width="100%">
            
            <tr bgcolor="#F4BAD8">
                <td colspan="2">
                    <h4>
                        Current Subscriptions</h4>
                </td>
            </tr>
            <tr bgcolor="#F4BAD8">
                <td>
                    Group
                </td>
                <td>
                    <select name="group" id="group" onchange="testChoice(29);">
                        <option value="-1">[SELECT]</option>
                        <option value="58">DFCoGroup</option>
                        <option value="60">FrankBobGroup</option>
                        <option value="68">SithTGroup</option>
                        <option value="70">CatalystGroup</option>
                        <option value="55">MadsoftGroup</option>
                    </select>
                </td>
            </tr>
        </table>
    </form>
    <form action="[URL unfurl="true"]http://aphrodite/cgi-bin/osmos.exe/savesubscription"[/URL] name="addnew"
        method="post" id="addnew">
        <table class="post" bgcolor="#F4BAD8" width="100%">
            <tr>
                <td>
                    <input id="name1" name="name1" value=""><p/>
                    <input id="start" name="start" value=""><p/>
                    <input id="expires" name="expires" value=""><p/>
                    <input id="custom" name="custom" value=""><p/>
                    <input id="iscurrent" name="iscurrent" value=""><p/>
                </td>
            </tr>
            <tr>
                <td>
                    <input id="address" name="address" value="">
                </td>
            </tr>
        </table>
    </form>
</body>

XML
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <data>
    This is some sample data. It is stored in an XML file and retrieved by JavaScript.
  </data>
  <name>
    Free
  </name>
  <datestarted>
25/10/2006 1:26:09 p.m.
  </datestarted>
  <dateexpires>
  </dateexpires>
  <custommonthlyrate>
  </custommonthlyrate>
  <iscurrent>
    1
  </iscurrent>
  <address>
    123 polar fleece lane
  </address>
</root>

cheers [cheers]

Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
You're obviously either a Perl or PHP programmer. You might try removing the illegal dollar sign from in front of your Javascript variable names and see what happens.

Lee
 
mmm sorry forgot to mention.. the code uses the $() function of prototype.js
function $() {
var elements = new Array();

for (var i = 0; i < arguments.length; i++) {
var element = arguments;
if (typeof element == 'string')
element = document.getElementById(element);

if (arguments.length == 1)
return element;

elements.push(element);
}

return elements;
}

Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
If i replace the $ with document.getElementById then i get the same error.

Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
As I understand it, you're asking for a node with a tagName of "name", and then asking for nodes [!]inside[/!] that node with a tagName of "datestarted"... but yet your XML doesn't have any nodes inside those named "name" - but rather as sibling elements.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top