Solved

Accessing form variable where variable name is a GUID

Posted on 2006-06-14
14
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 52

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 29

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 29

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
 
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 29

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 52

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 52

Expert Comment

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

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

724 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