Jump to content

What's wrong with my variable passing?


BliarOut

Recommended Posts

I know I'm just a full stop, a bracket or an apostrophe away from the answer to this but I can't see it. I have the following code which checks that a user has selected a radio button then opens a pop up window and passes a few variables to it. It works perfectly as pasted below:

 

//============================================================+
//Used to open 'popup' windows to display print documents.
//Takes the parameters url, name of the submitting form, the
//radio button name, the width, the height and the discipline ID
//============================================================+
function popup(url, frmName, ctrlName, hw, hh, typeID) 
{
//============================================================+
//Check if the radio button has been selected and get it's value.
//============================================================+
clickedOn='';
for (var i=0; i < document.PTWSelect.UniqueID.length; i++)

{
if (document.PTWSelect.UniqueID[i].checked)	{
	clickedOn='document.PTWSelect.UniqueID[i].value';
	var uniqueID = document.PTWSelect.UniqueID[i].value;
}
}
if (clickedOn == ""){
	alert("Please make a selection.")
	return false;
}
//============================================================+
//Set some variables for the popup.
//============================================================+
var width  = hw;
var height = hh;
var left   = (screen.width  - width)/2;
var top    = (screen.height - height)/2;
var params = 'width='+width+', height='+height;

params += ', top='+top+', left='+left;
params += ', directories=no';
params += ', location=no';
params += ', menubar=no';
params += ', resizable=yes';
params += ', scrollbars=yes';
params += ', status=no';
params += ', toolbar=no';
newwin=window.open(url+'?TypeID='+typeID+'&UniqueID='+uniqueID,'formWindow', params);
if (window.focus) {newwin.focus()}

return false;
}

//============================================================+
//End function.
//============================================================+

 

The problem is if I try to use a passed variable in this line it fails:

 

if (document.PTWSelect.UniqueID[i].checked)

 

What is the correct syntax, I thought this would work?

 

if (document.frmName.ctrlName.UniqueID[i].checked)

 

But it doesn't. If I test it by doing an alert(frmName +" "+ctrlName) the variables come through just as I expected. What am I missing?

 

Thanks,

 

Rob

Link to comment
https://forums.phpfreaks.com/topic/213560-whats-wrong-with-my-variable-passing/
Share on other sites

And what error(s) are you getting?

 

I think I know what the problem is, but not positive from your explanation.

 

"frmName" & "ctrlName" are variable names created from values passed into the function. You cannot use them like this:

if (document.frmName.ctrlName.UniqueID[i].checked)

 

When that code is encountered the javascript parser is looking for a form with the literal name "formName". It does not evaluate the value of the variable "formName". But. the solution is simple. Just use the object collection reference and use the variable accordingly.

 

Give this a try:

if (document.forms[frmName].elements[ctrlName].UniqueID[i].checked)

 

Although, I'm not sure that would work. What is UniqueID supposed to reference? It might help if you show the HTML form or at least the relevant part.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.