0

I have written a code where the admin has the access to change the general users from active to inactive and vice-versa. So I want to update the database if the radio button is changed from active to inactive.

Here is the code:

    <%
    while (rs.next()) {
        String firstname = rs.getString("firstname");
        String lastname = rs.getString("lastname");
        String email = rs.getString("email");
        String password = rs.getString("password");
        String age = rs.getString("age");
        String sex = rs.getString("sex");
        String haddress = rs.getString("haddress");
        String oaddress = rs.getString("oaddress");
        String phonenumber = rs.getString("phonenumber");
        String flag = rs.getString("flag");
        Statement stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                   ResultSet.CONCUR_READ_ONLY);


    %>



        <tr>
            <td>
              Name:  <% out.println(firstname); %> <% out.println(lastname); %>  

            </td>
            <td>
                <% if (flag.equals("A")){ %>

                Active: <input type="radio" value="A" name="<% out.println(email); %>"  onclick="<% stmt1.executeUpdate("update assignment set flag='A' where email='"+email+"'");
                               System.err.println("A"+email); %>
       alert('changed to active');" checked>
                Inactive: <input type="radio" value="I" name="<% out.println(email); %>" onclick="<% stmt1.executeUpdate("update assignment set flag='I' where email='"+email+"'");
                               System.err.println("I"+email); %>
       alert('changed to inactive');">
               <%
                    }else if(flag.equals("I")){%>

                Active: <input type="radio" value="A"  name="<% out.println(email); %>" onclick="<% stmt1.executeUpdate("update assignment set flag='A' where email='"+email+"'");
                               System.err.println("A"+email); %>
       alert('changed to active');">
                Inactive: <input type="radio" value="I" name="<% out.println(email); %>" onclick="<% stmt1.executeUpdate("update assignment set flag='I' where email='"+email+"'");
                               System.err.println("I"+email); %>
       alert('changed to inactive');" checked>

But in output, when I change one radio button, all the queries run automatically, updating the database to I for all the data in the DB.

2 Answers2

1

Using onclick, you need to invoke a ajax call and update the response . You should not use like onclick="<%SOME SQL Query%>". That is why it's updating all the rows.

Step 1: " onclick="updatestatus(this.value, '<% out.println(email); %>')"/>

Step 2 : Add Jquery lib and add a JS function

    function updatestatus(statuschg, email) {
       /*ajax call to some update-status.jsp with email and current value needs to be passed */
        //ajax code goes here ...
         $.ajax({
            type: "post",
            url: "update-status.jsp", //this is my servlet
            data: "email=" +email"&status="+statuschg,
            success: function(msg){
alert(msg); } }); }

Ref: How to invoke ajax call How to pass parameters in GET requests with jQuery

step 3: i) In update-status.jsp,Establish db connection ii) receive parameters of the email address and current status. iii) Execute the Query stmt1.executeUpdate("update assignment set flag='+status+' where email='"+email+"'") iv) out.println("Updated");

Community
  • 1
  • 1
Senthil
  • 2,156
  • 1
  • 14
  • 19
  • you can do that but page refresh happens and you need to retain the position. With out ajax, it won't be interactive / better user experience when the entire page reloads / refreshes. You can use like onchange="pagename.jsp?email="+email+"&status="+this.value; On refresh, get the values and do the db manipulation.But this will refresh every time, when u opt / change the opted value for each row. – Senthil Sep 22 '16 at 13:55
  • can you please elaborate the code for this? For me, regular refreshment won't be a problem. – Lavina Chhabra Sep 22 '16 at 14:53
  • 1. 2. In JS logic function pageupdate(emailid, optedval) { location.href='targetpage.jsp?email='+emailid+'&optedval='+optedval; } 3. in the JSP logic, use update query to receive the value and update it. – Senthil Sep 23 '16 at 07:16
  • I am having trouble with : '"+this.value+"' – Lavina Chhabra Sep 26 '16 at 06:18
  • use this, there is a typo in previous. – Senthil Sep 26 '16 at 06:45
  • I have edited the code as: Active: – Lavina Chhabra Sep 26 '16 at 07:39
0

You are mixing two different codes. The key is to realize, where and when each code is executed - JSP on the server when the page is requested and rendered (i.e. before the response is send to the browser) and Javascript in the browser, after the browser receives the already generated response.

I.e. in your

onclick="<% stmt1.executeUpdate("update assignment set flag='I' where email='"+email+"'"); System.err.println("I"+email); %> alert('changed to inactive');" 

the <% stmt1.executeUpdate("update assignment set flag='I' where email='"+email+"'"); System.err.println("I"+email); %> part is executed on the server when the page is generated, and only the alert('changed to inactive'); waits in browser until the input field is clicked.

What you need is AJAX.

Side note: Do not put Java code into JSP, use MVC pattern, or at least put the code into an "util" class and call the class from JSP.

Community
  • 1
  • 1
Jozef Chocholacek
  • 2,874
  • 2
  • 20
  • 25