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);
 }
 %>









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
Part refresh without a page refresh
Servlet refresh page
How To Page Refresh Using JavaScript In JSP
refresh jsp page - Ajax
servelt refresh page
ModuleNotFoundError: No module named 'forman'
ModuleNotFoundError: No module named 'formic'
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?
ModuleNotFoundError: No module named 'formly'
ModuleNotFoundError: No module named 'forms2'
ModuleNotFoundError: No module named 'formly'
ModuleNotFoundError: No module named 'forms2'
page refresh - JSP-Servlet
Click on Page Refresh - JSP-Servlet
Refresh a Web Page Using In Servlet
ModuleNotFoundError: No module named 'django-formit'
ModuleNotFoundError: No module named 'formdy-core'
Page Refresh - Struts
How to display content in same page while selecting fron dropdown list
How to refresh Client only once by sending setHeader ("refresh" ,??? )
How to display content in same page while selecting fron dropdown list
ModuleNotFoundError: No module named 'former'
Drop Down Reloads again in IE..How to prevent this?
ModuleNotFoundError: No module named 'formal'
ModuleNotFoundError: No module named 'formal'
ModuleNotFoundError: No module named 'formal'
ModuleNotFoundError: No module named 'formas-de-pago'
ModuleNotFoundError: No module named 'formas-de-pago'
ModuleNotFoundError: No module named 'formas-de-pago'
how can i prevent duplicate records using servlets and MVC model?
Page is not getting loaded when I refresh the webpage???
how can we prevent back option after log out.
how to prevent user selecting past date from calender
how to refresh google map using ajax by fetching lat long from database
How to prevent adding duplicate items to the shopping cart
how to genrate login id and password after submitting the sign up form?
Date Formay - Date Calendar
How to refresh a jTable On adding or deleting record ....
How to print this Format exactly?
how to print pdf format
how to prevent from navigating Back after logout or session expire in jsp
How do I change the while loop in this code to the range with range list style display page for a resultSet() in jsp?
How to download files from server to local disk using ZIP format in JSP page - JSP-Servlet
How to format number in Java?
Submitting Web site to search engine

Ads