2014-09-06 114 views
0

我必須根據檢查的類別選擇子類別。我堅持選擇多個複選框並使用ajax顯示其值。 我想使用純Ajax而不是jQuery。我從數據庫表中獲取1複選框的值,現在我需要顯示其他複選框與選擇查詢取決於取決於多個複選框用戶檢查的值。我有一個想法foreach循環將被使用,但不能理解如何以及在哪裏框架..請幫助。謝謝你。 這是以下形式:使用AJAX,PHP和MYSQL在另一個複選框中的html表單複選框顯示多個值

<?php 
while($f1=mysql_fetch_row($res)) { 
?> 
<input type="checkbox" name="chkcat[]" id="chkcat" onChange="showUser(this.value)" value='<?php echo $f1[1]; ?>'> <?php echo $f1[0]; ?> 
<? } ?> 
<div> id="txtHint"> </div> 
具有showUser功能

<script> 
function showUser(str) { 
if (str=="") { 
document.getElementById("txtHint").innerHTML=""; 
return; 
} 

if (window.XMLHttpRequest) { 
// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} else { // code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.onreadystatechange=function() { 
if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
} 
} 
xmlhttp.open("GET","ajax_chkbox.php?q="+str,true); 
xmlhttp.send(); 
} 
</script> 

並具有根據所選擇的類別和URL即獲取子類別文件

Ajax代碼:ajax_chkbox.php是:

while($row = mysql_fetch_array($result)) 
{ 
echo "<input type=checkbox name=chksubcat[] id=chsubkcat value= $row[0]> $row[2]"; 
echo "<br>"; 
} 
+1

我給你的建議的第一位是,分離出你的邏輯。 PHP真的應該只做服務器端處理。 jQuery - DOM操作。 KnockoutJS - UI中的數據綁定。您可能會發現爲應用程序的不同部分使用不同的庫會更容易。它更容易測試,更容易調試 – 2014-09-06 09:29:18

+0

嗯好吧,但我真的不知道.. – k2793 2014-09-06 09:36:18

+0

「我想使用純阿賈克斯而不是jquery」? Ajax不是圖書館 – 2014-09-06 09:38:29

回答

0

您可以更改下拉菜單複選框

小號elect_cat.php

<script type="text/javascript" src="http://ajax.googleapis.com/ 
ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() 
{ 
$(".category").change(function() 
{ 
var id=$(this).val(); 
var dataString = 'id='+ id; 

$.ajax 
({ 
type: "POST", 
url: "select_subcat.php", 
data: dataString, 
cache: false, 
success: function(html) 
{ 
$(".subcat").html(html); 
} 
}); 

}); 

}); 
</script> 

類別:

<select name="category" class="category"> 
<option selected="selected">--Select Category--</option> 
<?php 
include('databasefile'); 
mysql_connect($server,$username,$password)or die(mysql_error()); 
mysql_select_db($database)or die(mysql_error()); 
$sql=mysql_query("select cat_name from category order by cat_name"); 
while($row=mysql_fetch_array($sql)) 
{ 
$cname=$row['cat_name']; 
echo '<option value="'.$cname.'">'.$cname.'</option>'; 
} ?> 
</select> <br/><br/> 

SubCategory : 
<select name="subcat" class="subcat"> 
<option selected="selected">--Select SubCat--</option> 
</select> 

2.select_subcat.php

<?php 
include('databasefile); 
mysql_connect($server,$username,$password)or die(mysql_error()); 
mysql_select_db($database)or die(mysql_error()); 
if($_POST['id']) 
{ 
$id=$_POST['id']; 
$sql=mysql_query("select s_name from subcat_l1 where cat_name='$id'"); 
while($row=mysql_fetch_array($sql)) 
{ 
$sname=$row['s_name']; 
echo '<option value="'.$sname.'">'.$sname.'</option>'; 
} 
} 
?> 
SubCategory : 
<select name="subcat" class="subcat"> 
<option selected="selected">--Select SubCat--</option> 
</select> 
相關問題