hello friends i created a form and details of city & locations have to be come from database. if city hyderabad chosen then locations of hyderabad only appear in location selectbox.plz send code it is more helpful tome.
1)city.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="location.jsp";
url +="?city=" +str;
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChange(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("location").innerHTML=xmlHttp.responseText
}
}
</script>
</head>
<body>
<select name='city' 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 city");
while(rs.next()){
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>
<%
}
%>
</select>
<br>
<div id='location'>
<select name='location' >
<option value='-1'></option>
</select>
</div>
</body>
</html>
2)location.jsp:
<%@page import="java.sql.*"%>
<%
String city=request.getParameter("city");
String buffer="<select name='location' ><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 location where cityid='"+city+"' ");
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 `city` (
`cityid` bigint(255) NOT NULL auto_increment,
`cityname` varchar(255) default NULL,
PRIMARY KEY (`cityid`));
CREATE TABLE `location` (
`locationid` bigint(255) NOT NULL auto_increment,
`cityid` int(255) default NULL,
`location` varchar(255) default NULL,
PRIMARY KEY (`locationid`));
thank u my dear friend.but i have a code like this will u plz edit and send to me in correct manner. i am waiting for it .if u send as much as possible it will helpful to me my dear friend thanks forever.
<html>
<head>
<title>demo</title>
<body>
<form method="POST" action="http://localhost/vtigercrm/modules/Webforms/
post.php">
<input type="hidden" value="Leads" name="moduleName" />
<table>
<tbody>
<tr>
<td><label>Last Name</label></td>
<td><input type="text" name="lastname" value="" /></td>
</tr>
<tr>
<td><label>First Name</label></td>
<td><input type="text" name="firstname" value="" /></td>
</tr>
<tr>
<td><label>Company</label></td>
<td><input type="text" name="company" value="" /></td>
</tr>
<tr>
<td><label>email</label></td>
<td><input type="text" name="email" value="" /></td>
</tr>
<tr><td><label>city</label></td><td colspan=1>
<select name="city">
<option>city</option>
<?php
$connect=mysql_connect("localhost","root","venkataz");
if(!$connect) die("access failed");
$select=mysql_select_db("vtigercrm530");
if(!$select) die("select failed");
echo "selected vtigercrm530";
$query="SELECT DISTINCT address_city FROM vtiger_users";
$result=mysql_query($query);
if(!$result) die("database access failed:" . mysql_error());
while($row = mysql_fetch_array($result))
{
echo "<option value>".$row['address_city']."</option>";
}
?>
</select>
</tr>
<tr><td><label>lane</label></td><td colspan=1>
<select id="lane" onselect="show();">
<option>location</option>
<script type="text/javascript">
function show()
{
var lane=document.getElementById["lane"].selectedIndex;
alert(lane);
}
</script>
<?php
$connect1=mysql_connect("localhost","root","venkataz");
if(!$connect1) die("access failed");
$select1=mysql_select_db("vtigercrm530");
if(!$select1) die("select failed");
echo "";
echo "<br/>";
$query1="SELECT address_city FROM vtiger_users";
$result1=mysql_query($query1);
if(!$result1) die("database access failed:" . mysql_error());
$lcity=$_REQUEST["city"];
$rows=mysql_num_rows($result1);
for($j=0;$j<$rows;$j++)
if(mysql_result($result1,$j,"address_city")==$lcity)
{
$query2="SELECT address_street FROM vtiger_users WHERE address_city='".mysql_result($result1,$j,'address_city')."'";
$result2=mysql_query($query2);
while($row= mysql_fetch_array($result2))
{
echo "<option value>".$row['address_street']."</option>";
}
}
?>
</select>
</td></tr>
</tbody>
<table>
<input type="submit" value="Submit" />
</form>
</body>
</head>
</html>