How to keeping textarea content after a form submi

2019-09-11 02:19发布

问题:

I want to keep the textarea content in its filed after a form submition ... I work with jsp I try to do like this here solve to same problem but it not work with me my code :

 <form class="contact-form" id="preview-form"   action="textAreaData"  method="post">
 <textarea id="preview-form-comment" name="preview-form-comment">${fn:escapeXml(param.preview-form-comment)}</textarea>
 <input type="submit" name="preview-form=submit" id="preview-form-submit" value="Submit"  >
 </form>

and this is the result : display number 0 and when I submit the input it Disappears from its filed after a form submition

what wrong ? can help ?

回答1:

ok , the easy way :

in your servlet define :

String comment = request.getParameter("preview-form-comment") ;

request.setAttribute("anydata", comment);

and in your textarea write :

 <textarea id="preview-form-comment" name="preview-form-comment">${anydata}</textarea>

it run :) good luck



回答2:

One way to do this would be using php. I'll post an example below:

<input type="text" placeholder="Your name" name="name" value="<?php if(isset($_POST['name'])){echo $_POST['name'];}?>">

I'm not sure if you know php or not but essentially here's the breakdown. From your html it looks like you're self submitting meaning returning to the same page. So when you post data to the page we can access it using $_POST['name of the field']

That said we can use the isset php method to see if the form has posted data and if it has populate the field with the old value.