我能够获取所有值但是当第二次和第三次下拉通过Ajax时如何获取这些值?我想在不使用会话变量的情况下获取第二个和第三个下拉值。
我从数据库中获取下拉值,并且取决于接下来的2个下拉列表
<script>
$(document)ready(function()
{
$("#b").change(function()
{
var b=$(this).val();
$.ajax({url:"state.php", data:{id:b}, success: function(data)
{
$("#d").html(data);
}
});
});
$("#b1").click(function()
{
var a=$("#nm").val();
var b1=$("#m").val();
var c=$("#f").val();
var d=$("#c").val();
var e=$("#cpp").val();
var f=$("#php").val();
var g=$("#email").val();
var h=$("#pass").val();
var b=$("#b").val()
$.ajax({url:"formaction.php",data:{nm:a,b1:b1,c:c,d:d,f:f,email:g,pass:h,b:b}, success: function(data)
{
$("#d1").html(data);
}
});
});
});
</script>
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("arijitatest",$con);
$qr=mysql_query("select * from country");
?>
Name:<input type="text" name="nm" id="nm"><br>
Gender:<input type="radio" name="g" id="m" value="male">M
<input type="radio" name="g" id="f" value="female">F
<br>
Skill:<input type="checkbox" name="s[]" id="c" value="c">C
<input type="checkbox" name="s[]" id="cpp" value="cpp">CPP
<input type="checkbox" name="s[]" id="php" value="php">PHP
<br>
Country:
<select id="b" name="coun">
<option>select</option>
<?php
while($row=mysql_fetch_array($qr))
{
?>
<option value="<?php echo $row['cid']?>"> <?php echo $row['cname']?></option>
<?php
}
?>
</select>
<br>
<div id="d" name="d">
</div>
</select> <br>
Email id:<input type="text" name="email" id="email"> <br>
Password:<input type="password" name="pass" id="pass">
<br>
<input type="button" id ="b1" value="SUBMIT">
<input type="reset" value="CLEAR">
</form>
<div id="d1" name="d"></div>
this is the page for 2nd dropdown
<script>
$(document).ready(function()
{
$("#b1").change(function()
{
var b1=$(this).val();
$.ajax({url:"city.php", data:{sid:b1}, success: function(data)
{
$("#d2").html(data);
}
});
});
});
</script>
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("arijitatest",$con);
$cid=$_REQUEST['id']; //echo $cid;
$qr=mysql_query("select * from state where cid='$cid'");
?>
<select id="b1">
<option>select</option>
<?php
while($row=mysql_fetch_array($qr))
{
?>
<option value="<?php echo $row['sid']?>"><?php echo $row['sname']?></option>
<?php
}
?>
</select>
<div id="d2">
</div>
答案 0 :(得分:1)
我从数据库中获取下拉值,并取决于接下来的2个下拉列表。我几天前遇到了类似的问题所以好吧,让我们看看我们得到了什么。
我建议为config.php
创建一个单独的文件,而不是一直填充它。 PS。你将被要求很快使用mysqli / PDO,所以我只是把它留给小队并在我的例子中使用PDO。
dbconfig.php
<?php
$db_host = "localhost";
$db_user = "yourusernamehere";
$db_pass = "";
$db_name = "yourdbnamehere";
try
{
$db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
$DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
目前看起来非常混乱的
index.php
文件应该是 有点像:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_states.php",
data: dataString,
cache: false,
success: function(html)
{
$(".state").html(html);
}
});
});
$(".state").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_city.php",
data: dataString,
cache: false,
success: function(html)
{
$(".city").html(html);
}
});
});
});
</script>
</head>
<body>
<center>
<div>
<label>Country :</label>
<select name="country" class="country">
<option selected="selected">--Select Country--</option>
<?php
$stmt = $DB_con->prepare("SELECT * FROM tabel_country");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<option value="<?php echo $row['country_id']; ?>"><?php echo $row['country_name']; ?></option>
<?php
}
?>
</select>
<label>State :</label> <select name="state" class="state">
<option selected="selected">--Select State--</option>
</select>
<label>City :</label> <select name="city" class="city">
<option selected="selected">--Select City--</option>
</select>
</div>
<br />
</center>
</body>
</html>
get_city.php
<?php
include('dbconfig.php');
if($_POST['id'])
{
$id=$_POST['id'];
$stmt = $db_con->prepare("SELECT * FROM tbl_city WHERE state_id=:id");
$stmt->execute(array(':id' => $id));
?><option selected="selected">Select City :</option><?php
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<option value="<?php echo $row['city_id']; ?>"><?php echo $row['city_name']; ?></option>
<?php
}
}
?>
get_states.php
<?php
include('dbconfig.php');
if($_POST['id'])
{
$id=$_POST['id'];
$stmt = $db_con->prepare("SELECT * FROM tbl_state WHERE country_id=:id");
$stmt->execute(array(':id' => $id));
?><option selected="selected">Select State :</option><?php
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<option value="<?php echo $row['state_id']; ?>"><?php echo $row['state_name']; ?></option>
<?php
}
}
?>
这是一个有效的代码,所以你不应该有任何麻烦 周围很多,如果有什么你不理解,感觉 免费提出问题。