hi,i am trying to design a form on which 2 dependent drop down list is used my code is successful but when i select class from first drop down list all the data get lossed means the value entered in fields before drop down list get blank.what can i do for this?
This is the code
<script language="JavaScript"> function reload(form){ var val=form.vc.options[form.vc.options.selectedIndex].value; self.location='vehicleRegister1.php?vc=' + val ; } </script> <?php $con=mysql_connect("localhost","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("rtodb",$con); ?> <?php @$cat=$_GET['vc']; if(strlen($cat) > 0 and !is_numeric($cat)) { echo "Data Error"; exit; } $sql=mysql<em>query("SELECT clsid,veh</em>class FROM vehiclass"); if(isset($cat) and strlen($cat) > 0) { $quer=mysql<em>query("SELECT sbcid,veh</em>subclass FROM vehisubcls WHERE clsid = $cat"); } echo "<select name='vc' onchange=\"reload(this.form)\">"; echo "<option value=''>Select One</option>"; while ($data=mysql<em>fetch</em>assoc($sql)){ if($data['clsid']==$cat) { echo "<option selected value='$data[clsid]'>$data[veh_class]</option>"."<BR>"; } else { echo "<option value='$data[clsid]'>$data[veh_class]</option>";} } <br /> echo "</select>"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td height='31' id='t3'>"; echo "Vehicle Sub-Class"; echo "</td>"; echo "<td>"; echo "<select name='vsb'><option value=''>Select one</option>"; while($data1 = mysql_fetch_array($quer)) { echo "<option value='$data1[veh_subclass]'>$data1[veh_subclass]</option>"; } echo "</select>"; ?> </td> </tr> <td height="36" id="t3">Dealer Name </td> <td><select name="dnm"> <?php $sql="SELECT dealer_id,dealer_nm FROM dealer ORDER BY dealer_nm"; $result =mysql_query($sql); while ($data=mysql_fetch_assoc($result)){ ?> <option value ="<?php echo $data['dealer_id'] ?>" ><?php echo $data['dealer_nm'] ?></option> <?php } ?> </select>
Hi Friend,
Try this:
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); } %>
For the above code, we have created two database tables:
CREATE TABLE `country` ( `countryid` bigint(255) NOT NULL auto_increment, `countryname` varchar(255) default NULL, PRIMARY KEY (`countryid`)); CREATE TABLE `state` ( `stateid` bigint(255) NOT NULL auto_increment, `countryid` int(255) default NULL, `state` varchar(255) default NULL, PRIMARY KEY (`stateid`));
Thanks
Thank You Very much Please send me code in php
Hi everybody! The posted code was been very useful for me, but I have a question: how is possible to manage four dropdown dependnt list?
Thanks a lot. regards
Nico
Ads