Akanksha
Akanksha

Reputation: 25

get entries in servlet from jsp using edit and save via Javascript

Hi, I have a dynamic HTML table on my JSP page whose td is stored in textbox and becomes editable when I click on "EDIT" and the edit link changes to save and when I make changes and save, I want to save the entries to a servlet an get the values of these entries into servlet. I have the code for javascript that makes the rows editable on clicking edit and non-editable when I click save. I want to redirect my form to servlet via post method when I click save. How do i do that? Below is the code for form, which makes the table:

 <%
                        try{
                        out.println("<html>");
                        out.println("<form id ='submit34' method='post' action='updatealarm'>");
                        out.println("<table id='tblrt'class= 'cls' style='display;'   border=''><tr> <th>Tag</th> <th>Message</th>  <th>Severity</th> <th>Alarm Threshold</th> <th>Delay</th>  <th>Add to Group</th>  <th>Group</th>  <th>Condition</th>  <th>Conditional Tag</th>  <th>Condition Check</th>  <th>Condition</th>  <th>Email</th>  <th>Email To</th>  <th>Email Subject</th> <th>Email Message</th><th>Edit</th><th>Delete</th> </tr>");
                        Iterator<String> li = alarmarr.iterator();   
                        int flagy=1;
                        int temp = 1;

                        for (int f=0; f<=count; f++){
                            out.println("<tr>");
                            while(li.hasNext() && temp <=numberOfColumns){
                                temp++;
                                out.println("<td ><input id='tblrt' type='text' name='tagname' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text' name='msg' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text' name='severity'  value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input  id='tblrt' type='text'  name='threshold' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text'  name='delay' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text'  name='GroupCheck' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text' name='groupname'  value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text'  name='conditioncheck' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text'  name='conditiontagname' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text' name='conditionparams'  value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text'  name='conditionvalue' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text'  name='emailcheck' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text'  name='emailto' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt'type='text' name='emailsubject'  value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                out.println("<td><input id='tblrt' type='text'  name='emailmsg' value='"+(String)li.next()+"' disabled='disabled' /></td>");
                                temp++;
                                //storing the ID
                                String temporaryvalue = (String)li.next();
                                //out.println("<td><input type='text'  name='id' value='"+temporaryvalue+"' disabled='disabled' /></td>");

                                if(temp == 17 && flagy==1){
                                    out.println(" <td align='center'><a href='#' class='edit'>Edit</a>");

                                    out.println("<td id = 'delalarms' onclick= 'return myFunctionk(delalarms)' > <a  id='delalarms'  href='deletealarm?id="+temporaryvalue+"'>Delete</a></td>");

                                    flagy = 0;  
                                }
                            }
                            temp = 1;
                            flagy = 1;
                            out.println("</tr>");

                        }
                        out.println("</table></form></div>");
                        out.println("</center></div></body></html>"); 

                        }
                        catch(Exception e)
                        {
                            System.out.println(e);
                        }

                         %>

The javascript code that changes edit to save and back from save to edit

$(function(){
$('.checkall').on('click', function(){
    $(this).closest('.check-table').find(':checkbox').prop('checked',this.checked);
})

$(".check-table tr td:nth-child(5) a:last-child").on("click", function() {
    $(this).closest(".check-table tr").hide();
});

$('body').on('click','.edit', function(){
    if($(this).hasClass('on')){
        disabled = true;
        str = 'Edit';
        $(this).removeClass('on');

    }else{
         disabled = false;
        $(this).addClass('on');
        str = 'Save';
    }
     $(this).parents('tr').find('input').attr('disabled',disabled); 
     $(this).text(str);
});

})

Upvotes: 0

Views: 280

Answers (1)

developerwjk
developerwjk

Reputation: 8659

To submit a form via javascript using regular HTML submit (i.e. not ajax) just grab the form by id and call the submit function:

document.getElementById('submit34').submit();

Upvotes: 1

Related Questions