Php 抓取两个值
我从一个列表框中获取值并将其传递到另一个列表框,我所有的东西都使用一个值$Lid,但现在我需要两个$Lid和$Cid,这是正确的方法吗Php 抓取两个值,php,javascript,jquery,Php,Javascript,Jquery,我从一个列表框中获取值并将其传递到另一个列表框,我所有的东西都使用一个值$Lid,但现在我需要两个$Lid和$Cid,这是正确的方法吗 $(document).ready(function() { $(".Doggie").change(function() { var LocationString = $(this).find(":selected").val(); var CityString = $(this).find(":selected").val();
$(document).ready(function()
{
$(".Doggie").change(function()
{
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_city.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
});
$('.Kitty').live("change",function(){
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_area.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Pig").html(html);
}
});
});
});
</script>
</head>
<body>
<div id="frame1">
<label>Place :</label>
<select name="Doggie" class="Doggie" id="Doggie">
<option selected="selected">--Select Place--</option>
<?php
$sql = mysql_query("SELECT tblLocations.RestID as Lid, tblLocations.CityID as Cid, tblRestaurants.RestName as name
FROM tblRestaurants INNER JOIN tblLocations ON tblRestaurants.RestID = tblLocations.RestID
GROUP BY tblLocations.RestID, tblRestaurants.RestName
ORDER BY tblRestaurants.RestName ASC");
while($row=mysql_fetch_array($sql))
{
echo '<option value="'.$row['Lid'].''.$row['Cid'].'">'.$row['name'].'</option>';
} ?>
</select>
<label>City :</label>
<select name="Kitty" class="Kitty" id="Kitty">
<option selected="selected">--Select City--</option>
</select>
<label>Area: :</label>
<select name="Pig" class="Pig" id="Pig">
<option selected="selected">--Select Area--</option>
</select>
</div>
</body>
</html>
而且
<?php
require ('config.php');
if ($_POST['Lid']) {
$Lid = $_POST['Lid'];
$sql = mysql_query("SELECT tblLocations.RestId as Lid, tblLocations.CityID as Cid, tblCities.CityName as name
FROM tblLocations INNER JOIN tblCities ON tblLocations.CityID = tblCities.CityID
WHERE tblLocations.RestID = $Lid
GROUP BY tblLocations.RestID, tblCities.CityName
ORDER BY tblCities.CityName ASC");
echo '<option selected="selected">--Select City--</option>';
while ($row = mysql_fetch_array($sql)) {
echo '<option value="' . $row['Lid'] . '' . $row['Cid'] . '">' . $row['name'] . '</option>';
}
}
?>
现在它没有返回任何东西,所以我不得不假设它是错的。谢谢。我不明白为什么要在两个变量中存储相同的值:
var LocationString = 'Lid=' + $(this).val();
var CityString = 'Cid=' + $(this).val();
可以简化为:
var LocationString = $(this).val();
然后只有一个值,所以数据的格式如下
data: {
'Lid': LocationString
}
我不明白为什么在两个变量中存储相同的值:
var LocationString = 'Lid=' + $(this).val();
var CityString = 'Cid=' + $(this).val();
可以简化为:
var LocationString = $(this).val();
然后只有一个值,所以数据的格式如下
data: {
'Lid': LocationString
}
我建议进行以下更改:
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_city.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
您添加了两个数据值,这不是正确的方法。只需传递一个包含所需键和值的文本对象,并允许JQuery为您进行格式化。我建议您进行以下更改:
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_city.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
您添加了两个数据值,这不是正确的方法。只需传递一个包含所需键和值的文本对象,并允许JQuery为您进行格式化。数据应为格式
data: {Lid : LocationString, Cid : CityString},
并检查您的查询结果
核对一下
print_r(mysql_fetch_array($sql))
如果您的查询没有任何结果,则在while loop中进行echo将不起作用数据应为格式
data: {Lid : LocationString, Cid : CityString},
并检查您的查询结果
核对一下
print_r(mysql_fetch_array($sql))
如果您的查询没有任何结果,则在内部回送,而循环将不起作用
$(document).ready(function()
{
$(".Doggie").change(function()
{
var LocationString ='Rid='+ $(this).val();
$.ajax({
type: "POST",
url: "place_city.php",
data: LocationString,
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
});
$('.Kitty').live("change",function(){
var Rid = $('#Doggie').val(), // This is the value of the id="Doggie" selected option
Cid = $(this).val(); // This is the value of the id="Kitty" selected option
//alert("Rid = " + Rid + " Cid = " + Cid);
$.ajax({
type: "POST",
url: "place_area.php",
data: {"Rid":Rid,"Cid":Cid},
cache: false,
success: function (html) {
//alert('This is what is returned from the php script: ' + html);
$(".Pig").html(html);
}});});});
这就成功了
$(document).ready(function()
{
$(".Doggie").change(function()
{
var LocationString ='Rid='+ $(this).val();
$.ajax({
type: "POST",
url: "place_city.php",
data: LocationString,
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
});
$('.Kitty').live("change",function(){
var Rid = $('#Doggie').val(), // This is the value of the id="Doggie" selected option
Cid = $(this).val(); // This is the value of the id="Kitty" selected option
//alert("Rid = " + Rid + " Cid = " + Cid);
$.ajax({
type: "POST",
url: "place_area.php",
data: {"Rid":Rid,"Cid":Cid},
cache: false,
success: function (html) {
//alert('This is what is returned from the php script: ' + html);
$(".Pig").html(html);
}});});});
然而,我做了改变;仍然没有得到任何数据。第二段代码中的这一部分正确吗?如果$\u POST['Lid']{$Lid=$\u POST['Lid'];@ME dia你需要像我在回答中那样更改LocationString和CityString的值。@ME dia你做了一些不必要的事情,看看更新。你的php看起来没问题。LID和CID实际上是两个不同的值/我只是从tblLocation中拉出LocationID LID,但事实证明,我也需要CityID CID。这就是这两个原因。@ME dia您应该发布您的html,我认为问题在于您将更改事件绑定到的输入元素。$this.val不适用于选择框。但是,我做了更改;仍然没有获得任何数据。第二个代码段的这一部分正确吗?如果$\u post['Lid']{$Lid=$\u post['Lid'];@ME dia你需要像我在回答中那样更改LocationString和CityString的值。@ME dia你做了一些不必要的事情,看看更新。你的php看起来没问题。LID和CID实际上是两个不同的值/我只是从tblLocation中拉出LocationID LID,但事实证明,我也需要CityID CID。这就是这两个原因。@ME dia您应该发布您的html,我认为问题在于您将更改事件绑定到的输入元素。$this.val不适用于选择框。好的,我做了更改,仍然没有返回数据。这行是否正确?如果$\u post['Lid']{$Lid=$\u post['Lid'];@ME dia是类中的元素。doggie是一个选择框?如果是这样,请进行我刚才在$this上面所做的更改。find:selected.val;@ME dia还有,为什么要将相同的值赋给两个不同的变量?它应该是两个不同的值。LID和CID。一个是LocationID,另一个是CityID。我需要这两个值才能成功运行查询。O我做了更改,仍然没有返回数据。这行正确吗?如果$\u POST['Lid']{$Lid=$\u POST['Lid'];@ME-dia是类中的元素。doggie是一个选择框?如果是,请进行我刚才在$this上面所做的更改。find:selected.val;@ME-dia还有,为什么要将相同的值赋给两个不同的变量?它应该是两个不同的值。LID和CID。一个是LocationID,另一个是CityID。我需要这两个值才能成功运行查询。