0

Question - Re: R brew package with HTML (using Rook)

When using forms to retrieve queries, usually when the screen refreshes, the user's preselected option remains selected in the form. For eg., if there was a name field and I enter John,... enter other form data, hit Submit, when the page refreshes, the form will still show "John" in the name field instead of an empty box.

I can pre-fill an entry in a text type input field using something like --

    ...
    <input class="someclass" type="text" name="Name" value="<%=Name%>" />
    ... (other form data)

When the user hits submit and the page refreshes, R prefills the name field with the value from <%=Name%>

I'm having some difficult in replicating the same when using drop-down menus. The idea is that when the page refreshes, the option that the user had selected in the previous query will be active.

    <select class="someclass" name="group3" style="width:200px;">
    <option value="," selected>None</option>
    <option value="Name,">Name)</option>
    <option value="Phone">Phone</option>
    </select>

I think one way could be to enter something like say,

<%if (group3=="Name"){ print("selected") } ... etc for each of those options in the respective lines, but that is a bit cumbersome. Could you please share your thoughts on the same,

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
xbsd
  • 2,438
  • 4
  • 25
  • 35
  • Can you make the first value be something like: `` That would make the first value blank unless it is submitted, then it would make it the value submitted....I am sure there will be a better answer. – Seth Apr 10 '12 at 22:31
  • I guess you're using either RApache or Rook. – aL3xa Apr 10 '12 at 22:59
  • Yes - this is using Rook. I can try the method - but what happens is that it replicates the same option in the dropdown menu twice ... – xbsd Apr 11 '12 at 01:51

1 Answers1

0

Got this working using jQuery finally. The inline <%= if(..) print ("selected") ... within the tags wasn't working.

Add the id=something tag to select --

<select class="someclass" name="group3" id="myselect" style="width:200px;">
<option value=",">None</option>
<option value="Name">Name)</option>
<option value="Phone">Phone</option>
</select>

Add jquery.js to the html file in the header--

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

In my Rook brew file, I have something like,

forminput <- req$GET()
userselection <- forminput$group3 #group3 is the class name for the select statement

and in the HTML document, before the closing form tag, enter the following --

<script>
$("#myselect").val("<%=userselection%>").attr("selected", "selected");
</script>
xbsd
  • 2,438
  • 4
  • 25
  • 35