Jump to content

Recommended Posts

but I keep getting an error from Javascript saying it can't find the form...

 

Here's the html, the form I am trying to get is in the preferences div - updateprefs:

 

<div id="grouplist" style="display: none;"> This is the group list</div>
<div id="toolbox"> </div>
<div id="locator"> </div>
<div id="msgbox"> </div>
<div id="preferences"><form name="updateprefs"><label for "updateoption">Update Option</label><p><input type="radio" name="updateoption" value="inline"> Inline <input type="radio" name="updateoption" value="window" checked> Window</form></div>
<div id="header"> </div>
<div id="content"> </div>
<div id="newgroupwin" style="display: none;"><form name="newgroup"><label for "newgroupname">Enter New Group Name</label><input type="text" size="20" name="newgroupname"></form><p><a href="javascript:void(0);" onmousedown="makenewgroup();" style="padding: 1px; width: 150px; height: 25px; background-color: #333; color: #eee; border: 1px solid #666; text-decoration: none;">Submit</a>
<div id="update" style="display: none;"> </div>

 

The error message I am getting is:

 

Error: document.preferences is undefined

Source File: ...index6.php

Line: 15

 

Here's my javascript:

 

function showtable(view) { 

			var radiolength = document.forms[0].elements.length;
			xmlHttp = checkajax();

			if (view == "") {
				view = "edit"; 
			}

			xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				//alert(xmlHttp.responseText);
				var returneditems = xmlHttp.responseText.split("::");
				document.getElementById("header").innerHTML = returneditems[0];
				document.getElementById("content").innerHTML = returneditems[1];
				document.getElementById("toolbox").innerHTML = returneditems[2];
			}
		}

		xmlHttp.open("GET", "lib/maketable.php?view=" + view, true);
		xmlHttp.send(null);

		}

Link to comment
https://forums.phpfreaks.com/topic/183581-trying-to-get-radio-button-selections/
Share on other sites

Where/when is your JavaScript being written?  'Not defined' errors tend to be runtime problems, when the JavaScript is attempting to grab a reference of an element that hasn't been loaded into the browser yet.  You need to ensure that your HTML is loaded before you attempt to do anything with your script.

Thanks for the reply, I am running the function off of window.onload...

 

window.onload = showtable();


		//use this function to reload the table
		function showtable(view) { 

			var radiolength = document.forms[0].elements.length;
			xmlHttp = checkajax();

			if (view == "") {
				view = "edit"; 
			}

			xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				//alert(xmlHttp.responseText);
				var returneditems = xmlHttp.responseText.split("::");
				document.getElementById("header").innerHTML = returneditems[0];
				document.getElementById("content").innerHTML = returneditems[1];
				document.getElementById("toolbox").innerHTML = returneditems[2];
			}
		}

		xmlHttp.open("GET", "lib/maketable.php?view=" + view, true);
		xmlHttp.send(null);

		}

 

so everything should be loaded before the function runs...at least that's my understanding...

Hmm... Invoking it on the window.onload event shouldn't be causing any problems.

 

Do you have any other JavaScript with the line 'document.preferences' explicitly written anywhere?  I'm guessing that the line that's causing the thing to die is:

 

var radiolength = document.forms[0].elements.length;

 

But that shouldn't be causing a problem.

 

Is there a link to the page you can send me?

The page is internal so I can't link anyone to it.

 

Your correct on the line throwing the error. It gets a bit weirder if I rewrite the JS code as:

 

var radiolength = document.forms[0]

 

then everything loads fine. When I try to dig down into the elements is when the error is thrown. There's only one, the radio button and when I look at the html for that it looks ok to me.

 

This is even more frustrating...

 

var theform = document.forms[0]

 

this works...

 

var theform = document.forms[0]

var radiobtn = theform.updateoption

 

breaks on the second line, the error says theform is not defined...grrrrrrrr....

Just noticed a typo - remove the ()'s from the line:

 

window.onload = showtable();

 

So it reads as:

 

window.onload = showtable;

 

EDIT: why is it a typo?  onload is an event - event handlers are assigned to the event in question...the ()'s indicate invocation, which doesn't make sense in that context. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.