4
    <form name="myForm" method="post" onsubmit="return getComment()">
        <textarea id="commentarea"></textarea>
        <input type="text" name="locate" value="<%=rs.getString("location")%>">
        <input type="submit" value="View Comment">
    </form>


    function getComment(){
      <% String locate=request.getParameter("locate"); %>
      var location = <%= locate%>;
      document.getElementById('commentarea').value=location;
      return false;
    }

Everytime i click View Comment, there's no value printed. I want to access locate in the scriptlet and print the value in the text area. I know this is not the best way to access it, but i need to access it in this way. Can anyone help me?

joanna
  • 117
  • 1
  • 4
  • 12

1 Answers1

2

You have missed double/single quotes for the value of the location variable. If you don't need to submit the form, just use a button input element.

<form name="myForm" method="post">
        <textarea id="commentarea"></textarea>
        <input type="text" name="locate" value="<%=rs.getString("location")%>">
        <input type="button" value="View Comment" onclick="getComment()">
    </form>


function getComment(){
  <% String locate=request.getParameter("locate"); %>
  var location = "<%= locate%>";
  document.getElementById('commentarea').value = location;
}
Manjula
  • 4,961
  • 3
  • 28
  • 41
  • btw, it works but i always get a null value in the commentarea. – joanna Dec 07 '11 at 16:47
  • See the source of the page using "View source" option in your browser context menu. And check what is the value printed as the value of var location = "?"; ."locate" java variable is Null, I think. (And note that I corrected the event handle of input button to "onclick".) – Manjula Dec 07 '11 at 16:54
  • i want <%=rs.getString("location")%> to be the value of var location, what should i do? – joanna Dec 07 '11 at 17:08
  • 1
    You know that jsp is server side language and javascript is a client side language. Therefore when user request a page, server process the request and replace all those outputs of jsp variables with their actual values. Therefore what you need is to use "'rs.getString("location")" instead of "locate" inside those duble quotes of the var location value. I am sleepy. gtg. GN – Manjula Dec 07 '11 at 17:10
  • so, it should be var location = "<%= rs.getString("location")%>";? please help me, this is the last thing i need in my project. – joanna Dec 08 '11 at 11:39
  • Yes :O You should have tested that and check. It is better to check the value is null before printing there though. Otherwise you will get "null" string as the value of "location" javascript variable. Is that "rs" a database result set? – Manjula Dec 08 '11 at 12:43