Hi,

I have done code for two dorpdownlist by using Ajax. It is working properly on my machine but whenever copy to other machine that Ajax code not working, so please give me solution.


Thanks in advance

Recommended Answers

All 4 Replies

code?

Please provide some code for us to look at. Without seeing the code, it is impossible to troubleshoot the issue.

Hi,

This is jsp page.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page  import="java.sql.*,java.util.*,java.util.Date,java.text.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
       <link href="style.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"/>
<script type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
<script type="text/javascript" src="index.js"> </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Home Page</title>
        <script type="text/javascript">

            //window.onload="hideReturn()"
            function hideReturn()
                {
                    document.getElementById("noPaengersRe").style.display="none"
                    document.getElementById("listNumOfPersonRe").style.display="none"
                }

 
            function ListOfDest(selectedCity)
            {
               // alert("function load")
                xmlHttp = GetXmlHttpObject();
               // alert("xml obj is "+xmlHttp)
                if(xmlHttp == null)
                    {
                        alert("Browser does not support HTTP Request")
                        return
                    }
                var url = "DestinationRe"
                url = url+"?cityFrom="+selectedCity
               // alert("step url is "+url)
                xmlHttp.onreadystatechange=stateChanged
                xmlHttp.open("GET",url,true)
               // alert("step 2")
                xmlHttp.send()
               // alert("step 3")
            }
            function stateChanged()
            {
            //    alert("state is load")
                document.getElementById("listTo").options.length=0;
                AddItemlistTo("----Select City----","----Select City----")
               // if (xmlhttp.readyState==4 && xmlhttp.status==200)
                if(xmlHttp.readyState == 4 && xmlHttp.status==200)
                    {
                        //alert("step 4")
                        var strCity = xmlHttp.responseText;
                        //alert("str city is "+strCity)
                        var listCity = strCity.split(":");
                        //var listCity = strCity.split(":");
                     //   alert("list city is "+listCity)
                        if(listCity.length == 1)
                            {
                                AddItemlistTo(strCity,strCity)
                                document.getElementById("listTo").focus()
                            }else{
                                var len = listCity.length;
                                for(var i = 0;i<len;i++)
                                    {
                                       // alert("Item is "+listCity[i]+" length is "+listCity[i].length+" value of i is "+i)
                                        if(i != len-1)
                                            {
                                                AddItemlistTo(listCity[i],listCity[i])
                                            }else{
                                                var lenCity = listCity[i].length;
                                              //  alert("lenCity is "+lenCity)
                                                var lastCity = listCity[i].substring(0,lenCity-2)
                                            //    alert("lastCity len is "+lastCity.length)
                                                AddItemlistTo(lastCity,lastCity)
                                            }
                                    }
                                    document.getElementById("listTo").focus()
                            }
                    }
            }

            function GetXmlHttpObject()
            {
                var xmlHttp = null;
                try
                {
                    // Firefox, Opera 8.0+, Safari
                    xmlHttp = new XMLHttpRequest();
                }
                catch(e)
                {
                    try
                    {
                        xmlHttp = new ActiveXObject("Msxl2.XMLHTTP");
                    }
                    catch(e)
                    {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
                    }
                }
                return xmlHttp;
            }

            function AddItemlistTo(Text,Value)
            {
                // Create an Option object

                var opt = document.createElement("option");

                // Add an Option object to Drop Down/List Box
                document.getElementById("listTo").options.add(opt);

                // Assign text and value to Option object
                opt.text = Text;
                opt.value = Value;
            }
            function EmptylistTo()
            {
                var opt1 = document.createElement("option");
                document.getElementById("listTo").options.add(opt1);
                opt.text = "----Select City----";
            }
        </script>
    </head>

    <body onload="hideReturn()">

         <form name="frmIndex" action="SendBusInfo" method="post">

   <%!
        Connection con = null;//SendBusInfo
        Statement stmtSelectSource;
        ResultSet rsSelectSource;
        String sqlSelectSource="select distinct(Source) from BusMaster order by Source asc ";

        Statement stmtSelectDestination;
        ResultSet rsSelectDestination;
        String sqlSelectDestination="select distinct(Destination) from BusMaster order by Destination asc ";

    %>

    <%
       con = (Connection) com.etravel.ConnectionClass.connection();
    %>

<jsp:include page="header.jsp" />
<div id="round_top" ></div>
<div id="content" class="fixed">
 <div id="maincontent" class="fixed">

<img src="images/travel.jpg" alt=".." width="200" height="200" class="img-left" />

 <table>
                        <tr>
                            <th colspan="100%" align="center">Book your ticket now!!!</th>
                        </tr>


                        <tr>
                            <td> <input type="radio" name="radioTrip" value="oneway" checked="checked" onclick="ch()" />One-way</td>
                             <td><input type="radio" name="radioTrip" value="round" onclick="ch()" />Round-Trip</td>

                        </tr>
             <tr>
                            <td>From</td>
                            <%
                             stmtSelectSource=con.createStatement();
                           rsSelectSource = stmtSelectSource.executeQuery(sqlSelectSource);%>
                           <td><select name="listFrom" id="listFrom" onchange="ListOfDest(this.value);">
                          <option>----Select City----</option>
                      <%while (rsSelectSource.next())
                         {%>
                         <option><%=rsSelectSource.getString(1)%></option>
                    <%}%>
</select>
                        </td>
                </tr>
                        <tr>
                            <td>To</td>
                            <%
                             stmtSelectDestination=con.createStatement();
                           rsSelectDestination = stmtSelectDestination.executeQuery(sqlSelectDestination);%>
                           <td><select name="listTo" id="listTo">
                          <option>----Select City----</option>
                 <%--     <%while (rsSelectDestination.next())
                         {%>
                         <option><%=rsSelectDestination.getString(1)%></option>
                    <%}%>--%>
                                </select></td>
                        </tr>
<%
                    DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                    String currentDate = df.format(new Date());
                   %>
                        <tr>
                            <td>Date of Journey :</td>
                            <td><input type="text" value=<%=currentDate%> name="from" id="fromDate"/><input type="button" name="btnFrom" value="cal" onclick="displayCalendar(document.frmIndex.from,'yyyy/mm/dd',this)"/></td>
                            <%--<td><input type="text" name="txtDoj" /><%--<img src="images/calendar.jpg" alt="Starflower"/></td>--%>
                        </tr>
                        <tr>
                            <td>Date of Return :</td>
                            <td align="bottom"><input type="text" value=<%=currentDate%> name="to" id="toDate"   disabled/><input type="button" name="btnTo" value="cal"  disabled onclick="displayCalendar(document.frmIndex.to,'yyyy/mm/dd',this)"/></td>
                                <%--<td align="bottom"><input type="text" name="txtDor" /><input type="image" name="cal" src="images/calendar.jpg" alt="" width="20" height="20"/></td>--%>
                        </tr>
                        <tr></tr><tr></tr>
                        <tr><td align="center" colspan="100">No. of Pasenger</td></tr>
                        <tr></tr><tr></tr>

                        <tr>
                            <td colspan="100%" align="center">Onward :
                            <select name="listNumOFPerson" id="listNumOfPerson">
                                   <%-- <option>----Select----</option>--%>
                                    <option>1</option>
                                    <option>2</option>
                                    <option>3</option>
                                    <option>4</option>
                                </select>

                              Return :
                                <select name="listNumOFPersonRe" id="listNumOfPersonRe" disabled>
                                    <option>1</option>
                                    <option>2</option>
                                    <option>3</option>
                                    <option>4</option>
                                </select></td>

                        </tr>

                            <td colspan="100%" align="center"><img src="images/searchbuses.gif" height="20" width="100" alt="Continue" border="1" name="checkAvl" onclick="checkSelect()"/></td>



                        </table>

                        <input type="hidden" name="hdnIndex" value="index.jsp" />

 <a href="http://www.twitter.com/"><img src="images/twitter.jpeg" alt=".." width="100" height="50" /></a>
  <a href="http://www.google.com/"><img src="images/google.jpeg" alt=".." width="100" height="50"/></a>
  <a href="http://www.irctc.com/"><img src="images/irctc.jpeg" alt=".." width="50" height="50"/></a>
    <h3><blink>New Roots </blink></h3>
         <ol>
            <li> * Pune to Goa</li>
           <li> * Pune to Ahemdabad</li>
           <li> * Mumbai to Hyderabad</li>
         </ol>



<h3><blink>Sailent Features </blink></h3>
         <ol>

             <li> * Home delivery service of bus tickets in major cities</li>
           <li> * Return bus tickets reservation </li>
           <li> * Read bus reviews submitted by passengers</li>
           <li> * Fixed Schedule depatures</li>
         </ol>

<a href="http://www.yatra.com/"><img src="images/link.gif" alt=".." width="500" height="80" class="img-right" /></a>
 </div>

        <jsp:include page="sideBar.jsp" />
</div>
<div id="round_bottom" ></div>
          <jsp:include page="footer.jsp" />
         </form>
    </body>

</html>

This is Servlet page for calculate destination city.

package com.etravel;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.*;

/**
 *
 * @author node4
 */
public class DestinationRe extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        Connection con = null;
        Statement stmtDestination = null;
        ResultSet rsDestination = null;

        con = (Connection)ConnectionClass.connection();


        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String sourceDis = request.getParameter("cityFrom");
       // String destinationDis = request.getParameter("cityTo");

        String cityData = "";
       ArrayList listDestination = new ArrayList();
            try{
               // out.println("The source is "+sourceDis);
                String sqlDestination = "SELECT DISTINCT(Destination) FROM BusMaster WHERE Source='"+sourceDis+"'";
                stmtDestination = con.createStatement();
                rsDestination = stmtDestination.executeQuery(sqlDestination);
                int count = 0;

                while(rsDestination.next())
                {
                    listDestination.add(rsDestination.getString("Destination"));
                    count++;
                }
                 Iterator itrr = listDestination.iterator();



                 while(itrr.hasNext())
                 {

                   /* if(count == 1)
                    {
                        cityData = itrr.next() + ":";
                    }else{*/
                             if(cityData.equals(""))
                            {
                                cityData =(String) itrr.next();
                            }else{
                                   cityData = cityData + ":" + itrr.next();
                                 }
                  //  }

                 }
                 out.println(cityData);

                /*request.setAttribute("sndlistDestination", listDestination);
                request.setAttribute("sndsource", sourceDis);

                RequestDispatcher dispatcherDist = request.getRequestDispatcher("index.jsp");
                if(dispatcherDist != null)
                {
                    dispatcherDist.forward(request, response);
                }*/

            }catch(Exception e)
            {
                out.println("Excetion regards Destination is "+e);
            }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Please help me, I m waiting for reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.