2

I would like to get text from textarea and send it as a parameter in form action.

So, I have page showarticle.jsp which show me all article's comments. Firstly, I call it on index.jsp with parameter comment_ref=null(it means that I don't want to add comment for the first time)

<a href="showarticle.jsp?id=${row.id}&comment_ref=null">${row.title}</a>

In showarticle.jsp I have a form for adding new comment

<form action="showarticle.jsp?id=${id_param}&comment_ref=${comment_text_aria_cur}" method="post">
  <textarea name="comment_text_aria_cur" rows="4" cols="50"></textarea>
  <input type="submit" name ="submit" value="Send">
</form>

and here I again call showarticle.jsp, but here I want to add text from text area as a parameter comment_ref.

I've tried that

<form action="showarticle.jsp?id=${id_param}&comment_ref="<%=request.getParameter("comment_text_aria_cur") method="post">

but still doesn't work. In this case I can see only previous value. For example,

I write comment "aaa" and press submit -> comment_ref = null. I write comment "bbb" and press submit -> comment_ref = aaa.

I'm sure solution is very simple.Thank you!

2 Answers2

1

First change method="post" to method="get". Usually, Post doesn't allow the values to be sent through url (However, you can still achieve querystring using javascript). For post, the values are sent in the request body, in the format that the content type specifies (mostly it is application/x-www-form-urlencoded).

Once you change post to get, the values are automatically appended to the url when you submit the form. You need not append it explicitly.

request.getParameter is used to get value of form parameter i.e. take values of parameters from url request. You cannot use this to append values.

This is simple way to do.

<form action="showarticle.jsp" method="GET">
     <textarea name="comment_text_aria_cur" rows="4" cols="50"></textarea>
     <input type="hidden" name ="id" value="<%= request.getParameter("id_param")%>">
     <input type="submit" name ="submit" value="Send">
</form>

Make sure you are setting id_param (request.setAttribute("id_param", value) )

sree
  • 63
  • 6
  • POST method can support querystring data, although it's not common. In case you use it, you should use it as resource identification and not as submitted data (ref: https://stackoverflow.com/a/5876931/3652270). In this case, in my opinion, the comment should not be used as a querystring parameter. Cheers. – sanastasiadis Jul 09 '18 at 15:01
  • i've changed post on get and it entail syntax error(in sql statement) which absent before. – Tatyana Tatyana Jul 09 '18 at 19:48
  • sql statement syntax error is probably not related to get/post. Please check if the parameter values are fetched correctly and embedded in sql statement correctly. – sree Jul 09 '18 at 19:57
  • it works even without hidden type of input. thank you! – Tatyana Tatyana Jul 09 '18 at 21:20
1

Normally, you should pass the comment data as a post parameter, and not as querystring parameter. So, your code should look like this:

<form action="showarticle.jsp?id=${id_param}" method="post">
  <textarea name="comment_text_aria_cur" rows="4" cols="50"></textarea>
  <input type="submit" name ="submit" value="Send">
</form>

And then, from the target page showarticle.jsp, you should be able to call:

<% request.getParameter("comment_text_aria_cur") %>

Of course this will return the value that was submitted to the current page with your last request. It can be used to process and/or view the value of the previously submitted comment. On the other hand, it should not be used to format your form's action string for the next request.


However, if you insist to change your querystring with some data when you submit your form, you could do something like this, using javascript:

<html>
<body>
<form name="myForm" onsubmit="submitForm();" action="showarticle.jsp" method="post">
  <input type="text" name="id" />
  <textarea name="comment_text_aria_cur" rows="4" cols="50"></textarea>
  <input type="submit" name ="submit" value="Send">
</form>

<script>
function submitForm() {
    form = document.forms["myForm"];
    form.action = form.action+"?id="+form["id"].value+"&comment_ref="+form["comment_text_aria_cur"].value;
    return true;
}
</script>
</body>
</html>

This includes:

  1. giving a name to your form so you can access it from javascript
  2. providing an onsubmit method to be executed when you submit your form
  3. in the javascript event method you can get the values of the parameters and format a new action string with the querystring parameters you want
  4. set the new string to the form action
sanastasiadis
  • 1,182
  • 1
  • 15
  • 23
  • thank you! but don't want include javascript to my project. i suppose that java has enough opportunities. but i will if don't find the solution – Tatyana Tatyana Jul 09 '18 at 15:00