Solved

Accessing form variable where variable name is a GUID

Posted on 2006-06-14
14
295 Views
Last Modified: 2012-05-05
I am pulling questions out of a databse for an online survey.  The variable names in the html form are GUID's.

for example

<input type="text" name="{cccccccc-cccc-cccc-cccc-cccccccccccc}">

I want to check the form variables with javascript, but when i try and access the variable:

document.formname.{cccccccc-cccc-cccc-cccc-cccccccccccc}.value

I get an error message.

I tried to assign the GUID to a variable as a string:
varname = "{cccccccc-cccc-cccc-cccc-cccccccccccc}"

then access the form variable:
document.formname.varname.value

...but that didn't work either.

Any suggestions?

Thanks in advance
0
Comment
Question by:bizkit112
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 16908911
try use document.getElementById method ? like:

varname = "{cccccccc-cccc-cccc-cccc-cccccccccccc}"

then:

document.getElementById(varname).value ...
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16909003
The name has to be a String because of the special characters.
Then you can use that String in one of two ways:
Either:
document.formname["{cccccccc-cccc-cccc-cccc-cccccccccccc}"].value

Or:
document.formname[varname].value




0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16910536
Another way could be loop over (it is inefficient, but works).

<html>
<head>
<title>Form Field Eval</title>
</head>
<body>
<script language="javascript">
function catchErr(err) {
      alert (err);
}
onerror = catchErr;
function GetValue (form, fldName) {
  for (ix=0; ix < form.elements.length; ix++) {
     if (form.elements[ix].name.match(fldName)) {
      alert (form.elements[ix].value);
      return (form.elements[ix].value);
     }
  }
 
}
</script>
<form name="form1">
<input type="text" name="{cccccccc-cccc-cccc-cccc-cccccccccccc}">
<input type="button" value="Get Value of GUID"
onclick='GetValue(this.form,"{cccccccc-cccc-cccc-cccc-cccccccccccc}");'>
</form>
</body>
</html>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16910982
Sorry, but what your GetValue() does is identical to: this.form["{cccccccc-cccc-cccc-cccc-cccccccccccc}"].value;
So why the error-prone loop? Don't you know that match() does also match a subset?
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16911976
(form.elements[ix].name == fldName) does the exact match check.
Thanks Zvonko for helping me point out a possible flaw in my code.



<html>
<head>
<title>Form Field Eval</title>
</head>
<body>
<script language="javascript">
function catchErr(err) {
     alert (err);
}
onerror = catchErr;
function GetValue (form, fldName) {
  for (ix=0; ix < form.elements.length; ix++) {
     if (form.elements[ix].name == fldName) {
     alert (form.elements[ix].value);
     return (form.elements[ix].value);
     }
  }
 
}
</script>
<form name="form1">
<input type="text" name="{cccccccc-cccc-cccc-cccc-ccccccccccccccc}">
<input type="text" name="{cccccccc-cccc-cccc-cccc-cccccccccccc}">
<input type="button" value="Get Value of GUID"
onclick='GetValue(this.form,"{cccccccc-cccc-cccc-cccc-cccccccccccc}");'>
</form>
</body>
</html>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912040
Flaw or not flaw, the loop is NOT necessary.
0
 

Author Comment

by:bizkit112
ID: 16917690
Here are the results I had...

when i use the following code:

varname = "{cccccccc-cccc-cccc-cccc-cccccccccccc}"
if (document.healthhistoryform.getElementByID(varname).value == '')

i get error: Object doesn't support this property or method

when i use:

if (document.healthhistoryform["{cccccccc-cccc-cccc-cccc-cccccccccccc}"].value == '')

i get the error:  '{GUID}.value' is null or not an object

and i know the form variable is not null.

Thanks for your help so far, are there any other suggestions?

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 63

Expert Comment

by:Zvonko
ID: 16918055
Do you have the correct UpperCase and LowerCase spelling in healthhistoryform name and {cccccccc-cccc-cccc-cccc-cccccccccccc} field name? Best copied by copy'n paste.

0
 

Author Comment

by:bizkit112
ID: 16947660
Yes, the case is correct.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16948600
OK, the form element does not support the getElementByID() method, and even when you correct would call the document.getElementById() you cannot get a form element name when it has not the same id as name.
Can wee see your page source or at least the concerned parts? Or is your test page online?

0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16982968
I have tried my post ???

0
 

Author Comment

by:bizkit112
ID: 17066769
<script>
<!--

function checkForm()
{
      var validForm = false;
      alert("here");
      varname = "{66cd89ce-4326-43b9-a9ac-f3c5858901cc}"
      if (document.getElementByID(varname).value == '')
      {
            validForm = false;
            alert("it worked");
      }
      alert("after");
}


// -->
</script>

<form name="healthhistoryform" method="post" onsubmit="return checkForm()" action="#">
<table width="760" border="0" cellpadding="0" cellspacing="0">
      <tr>
            <td width="25"><%=i%></td>
            <td width="250"><%=HealthHistory("Question")%></td>
            <td><input type="<%=HealthHistory("Type")%>" name="<%=HealthHistory("GUID")%>" id="<%=HealthHistory("GUID")%>"></td>
      </tr>
</table>
</form>


Here, the GUID in the javascript function matches the HealthHistoryForm("GUID") value in the input field.
It did not work with the getElementByID, as mentioned before, possibly b/c it has the same value for name as it does for id.

I get the following error:
     Object doesn't support htis property or method

on this line of code:
     if (document.getElementByID(varname).value == '')
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 17066798
Javascript is case sensitive, try use document.getElementById instead of document.getElementByID, like:


function checkForm()
{
     var validForm = false;
     alert("here");
     varname = "{66cd89ce-4326-43b9-a9ac-f3c5858901cc}"
     if (document.getElementById(varname).value == '')
     {
          validForm = false;
          alert("it worked");
     }
     alert("after");
}
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 17066801
ah, this is same as what Zvonko already mentioned above.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now