简体   繁体   中英

how to create dynamically html form

When the user enter number like

int number=10;

Based on that number i need to create 10 html forms

I tried following code in jsp but it is not working in case of validation

<%   
int n1=10;
for(int i=1;i<=n1;i++)
{
%>
<form action="Store1.jsp" method="post" >

<pre>
Enter the First Name  :  <input id="FName" type="text" name="FName"> <br>
Enter the Middle Name :  <input id="MName" type="text" name="MName"> <br>

  <input type="button" value="Save" >  
  <input type="button"value="Edit" >  

</pre>      

<%
}
%>

<input type="submit" Value="Submit">
</form>

How can we create such html forms?? and provide validation to it??

use this:

<form action="Store1.jsp" method="post" >
<%   
int n1=10;
for(int i=1;i<=n1;i++)
{
%>
   <pre>
    Enter the First Name  :  <input id="FName<%=i; >" type="text" name="FName<%=i; %>"> <br>
    Enter the Middle Name :  <input id="MName<%=i; %>" type="text" name="MName<%=i; %>"> <br>

    <input type="button" value="Save" >  
    <input type="button"value="Edit" >  

   </pre>
<%
}
%>

<input type="submit" Value="Submit<%=i; %>" name="Submit">
</form>

you will have unique identifier for each field. so basically you just loop through the fields, and check for FName0, FName1, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM