php jquery ajax 二级联动下拉菜单,但我的代码感觉没有执行到php里,请高手们帮看看问题出在哪里,谢谢
tpl页面:<formaction="published-work.php"method="get"id="createform"name="createform"><s...
tpl页面:
<form action="published-work.php" method="get" id="createform" name="createform">
<select name="category" id="category">
__(foreach name=all_category_information from=$all_category_information item=category_information)__
<option value="__($category_information.pinyin)__">__($category_information.name_curlang)__</option>
__(/foreach)__
</select>
<br/>
<select name="service_require" id="service_require">
</select>
<script language = "JavaScript">
function getSelectVal(){
$.getJSON("published-work.php",{task:"service_require"},function(json){
alert("88888");
var service_require = $("#service_require");
$("option",service_require).remove(); //清空原有的选项
$.each(json,function(index,array){
var option = "<option value='"+array['id']+"'>"+array['name_curlang']+"</option>";
service_require.append(option);
});
});
}
$(function(){
getSelectVal();
$("#category").change(function(){
getSelectVal();
});
});
</script>
</form>
php代码:
<?php
if($_GET["task"] == 'service_require'){
$category_code = $p_category;
if(isset($category_code)){
$sql = mysql_query("select * from cms_channel where code=$category_code");
while($row=mysql_fetch_array($sql)){
$select[] = array("id"=>$row[channel_id],"name_eng"=>$row[name_eng]);
}
echo json_encode($select);
exit;
}
}
?> 展开
<form action="published-work.php" method="get" id="createform" name="createform">
<select name="category" id="category">
__(foreach name=all_category_information from=$all_category_information item=category_information)__
<option value="__($category_information.pinyin)__">__($category_information.name_curlang)__</option>
__(/foreach)__
</select>
<br/>
<select name="service_require" id="service_require">
</select>
<script language = "JavaScript">
function getSelectVal(){
$.getJSON("published-work.php",{task:"service_require"},function(json){
alert("88888");
var service_require = $("#service_require");
$("option",service_require).remove(); //清空原有的选项
$.each(json,function(index,array){
var option = "<option value='"+array['id']+"'>"+array['name_curlang']+"</option>";
service_require.append(option);
});
});
}
$(function(){
getSelectVal();
$("#category").change(function(){
getSelectVal();
});
});
</script>
</form>
php代码:
<?php
if($_GET["task"] == 'service_require'){
$category_code = $p_category;
if(isset($category_code)){
$sql = mysql_query("select * from cms_channel where code=$category_code");
while($row=mysql_fetch_array($sql)){
$select[] = array("id"=>$row[channel_id],"name_eng"=>$row[name_eng]);
}
echo json_encode($select);
exit;
}
}
?> 展开
1个回答
展开全部
很明显,你没传过参数来,应该把var service_require = $("#service_require");
弄到$.getJSON行上面
然后,把service_require,传到php
$.getJSON("published-work.php",{task:service_require},function(json){
再有就是:$select[] = array("id"=>$row[channel_id],"name_eng"=>$row[name_eng]);
数组的键应该加引号$row[“channel_id”],$row[’name_eng‘]
弄到$.getJSON行上面
然后,把service_require,传到php
$.getJSON("published-work.php",{task:service_require},function(json){
再有就是:$select[] = array("id"=>$row[channel_id],"name_eng"=>$row[name_eng]);
数组的键应该加引号$row[“channel_id”],$row[’name_eng‘]
追问
我试了还是不行,jquery方法中我有alert("88888");但是都没有执行。
为什么把service_require传到php?task:service_require是在php判断的参数.数组加上引号了;
是不是php中$_GET['']拿不到值呢?php中直接拿第一个select的值$category_code = $p_category;这样应该不会有错啊.
追答
那这样,简单点,吧$.getJSON 改成$.get
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询