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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating radio buttons in JSP

Status
Not open for further replies.

danglingpointers

Technical User
Jul 29, 2005
18
US
What I would like to do is populate the radio button selections with results from a database query. Here is what I have thus far:

Code:
<% //begin JSP, call database %>
<form>
<table>
 <tr>
  <td><input type="radio" value="VAL1" name="button"></td>
  <td><input type="radio" value="VAL2" name="button"</td>
  <td><input type="radio" value="VAL3" name="button"</td>
 </tr>
</table>

<table>
 <tr>
  <td><input type="radio" value="VAL1" name="button1"</td>
  <td><input type="radio" value="VAL2" name="button1"></td>
  <td><input type="radio" value="VAL3" name="button1"></td>
 </tr>

<script>

  document.forms[0].elements['button']['<%= rs.getString("COL_NAME") %>'].checked = true;
  document.forms[0].elements['button1']['<%= rs.getString("COL_NAME") %>'].checked = true;

</script>
</form>
<% // do some more java stuff. %>
When the page loads, I get an error "document.forms.0.elements.button.'VALUE_OF_rs.getString()' is null or not an object.

I have checked my rendered page source and the scriptlets are returning valid values for the radio button element choices. I have tried moving the javascript code to various places around the page and have come up with the same error.

Thanks in advance.

- Tim
 
My guess is that the error message is from the JSP processor, not from the Javascript. I have never seen a message like this
Code:
'VALUE_OF_rs.getString()' is null or not an object

and I have seen a lot of Javascript errors, believe me.

If the message is from JSP, then it may mean that the recordset is empty, or that the column has a NULL in the current row of the recordset.

Regarding the Javascript, I think the value must be an integer like
Code:
document.forms[0].elements['button'][5].checked = true;

because the elements of a radio group are accessible in an array which uses numerical values for the subscript.

 
rac2,

That was the problem indeed. The "'VALUE_OF_rs.getString()'" wasn't part of the actual error code, but rather that statement evaluated to a single string which was part of the error code. My apologies for the confusion.

Now all I have to do is find a way to enumerate all of the possible values of the element choices. If only I could cross-line javascript and c++...

- Tim
 
Could you use something along the lines of:

Code:
[b]<%
 while(rs.getNext())
 {
%>[/b]

<script>
[b]if(document.forms[0].elements['button']['<%= rs.getString("COL_NAME") %>'])
{[/b]
  document.forms[0].elements['button']['<%= rs.getString("COL_NAME") %>'].checked = true;
  document.forms[0].elements['button1']['<%= rs.getString("COL_NAME") %>'].checked = true;
[b]}[/b]
</script>
[b]<%
 }
%>[/b]

The resulting HTML might not be so very pretty, but as long as it doesn't lead to the generation of a thousand if-blocks, it shouldn't be a problem.

--Dave
 
Why don't you use:
Code:
><input type="radio" value="VAL1" name="button" checked="checked"/>
Instead of using JavaScript. Will work with people who have JavaScript turned off too!

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top