How to prevent the page refresh while submitting the form??

How to prevent the page refresh while submitting the form??

Hi. Requirement: There are two drop down boxes.Dropdown2 should load corresponding to the select of Dropdown1. Im hitting a db for this every time.Hence the page loads every time when changing Dropdown1. Is there any way in JAVA SCRIPT to prevent this page loading???

Thanks much!!

View Answers

June 20, 2011 at 11:00 AM

1)country.jsp:

 <%@page import="java.sql.*"%>
 <html>
      <head>  
      <script language="javascript" type="text/javascript">  
      var xmlHttp  
      var xmlHttp
      function showState(str){
      if (typeof XMLHttpRequest != "undefined"){
      xmlHttp= new XMLHttpRequest();
      }
      else if (window.ActiveXObject){
      xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
      }
      if (xmlHttp==null){
      alert("Browser does not support XMLHTTP Request")
      return;
      } 
      var url="state.jsp";
      url +="?count=" +str;
      xmlHttp.onreadystatechange = stateChange;
      xmlHttp.open("GET", url, true);
      xmlHttp.send(null);
      }

      function stateChange(){   
      if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
      document.getElementById("state").innerHTML=xmlHttp.responseText   
      }   
      }

        </script>  
      </head>  
      <body>  
      <select name='country' onchange="showState(this.value)">  
       <option value="none">Select</option>  
    <%
 Class.forName("com.mysql.jdbc.Driver").newInstance();  
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("Select * from country");
 while(rs.next()){
     %>
      <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>  
      <%
 }
     %>
      </select>  
      <br>  
      <div id='state'>  
      <select name='state' >  
      <option value='-1'></option>  
      </select>  
      </div>  
      </body> 
      </html>

2)state.jsp:

<%@page import="java.sql.*"%>
<%
String country=request.getParameter("count");  
 String buffer="<select name='state' ><option value='-1'>Select</option>";  
 try{
 Class.forName("com.mysql.jdbc.Driver").newInstance();  
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("Select * from state where countryid='"+country+"' ");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(3)+"</option>";  
   }  
 buffer=buffer+"</select>";  
 response.getWriter().println(buffer); 
 }
 catch(Exception e){
     System.out.println(e);
 }
 %>

Ads









Related Tutorials/Questions & Answers:
How to prevent the page refresh while submitting the form??
prevent browser refresh
Advertisements
how to refresh my GUI page
jsp page submitting
How To Page Refresh Using JavaScript In JSP
How to prevent from navigating to ck buttonnext page when user reaches login page using back button
How to prevent from navigating to a page after reaching Login page using back button?
servelt refresh page
How to display content in same page while selecting fron dropdown list
ModuleNotFoundError: No module named 'forman'
ModuleNotFoundError: No module named 'formic'
Refresh a Web Page Using In Servlet
How to display content in same page while selecting fron dropdown list
ModuleNotFoundError: No module named 'formly'
ModuleNotFoundError: No module named 'forms2'
ModuleNotFoundError: No module named 'formly'
ModuleNotFoundError: No module named 'forms2'
How to refresh Client only once by sending setHeader ("refresh" ,??? )
Drop Down Reloads again in IE..How to prevent this?
ModuleNotFoundError: No module named 'django-formit'
ModuleNotFoundError: No module named 'formdy-core'
how can i prevent duplicate records using servlets and MVC model?
how to prevent user selecting past date from calender
ModuleNotFoundError: No module named 'former'
How to prevent adding duplicate items to the shopping cart
how to refresh google map using ajax by fetching lat long from database
Page is not getting loaded when I refresh the webpage???
ModuleNotFoundError: No module named 'formal'
ModuleNotFoundError: No module named 'formal'
ModuleNotFoundError: No module named 'formal'
how to genrate login id and password after submitting the sign up form?

Ads