从另一个基于PHP的DropDownList自动填充DropDownList [英] Auto Populate DropDownList from another DropDownList based in PHP

查看:62
本文介绍了从另一个基于PHP的DropDownList自动填充DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图根据另一个下拉列表选择填充一个下拉列表,但没有得到结果.贝娄是我的代码,请告诉我我哪里错了.

控制器:index.php

Hi,

I am trying to populate a dropdownlist based on another dropdownlist selection and not getting the result. Bellow is my code, Please tell me where I am wrong.

The Controller: index.php

	$link = mysqli_connect('localhost', 'root', 'aweklin@topnetconsulting');
	if (!$link){
		echo 'There was an error connecting to database server: ' . mysqli_error($link);
		exit();
	}
	if (!mysqli_select_db($link, 'practice')){
		echo 'There was an error selecting practice database: ' . mysqli_error($link);
		exit();
	}
	
	$sql = "SELECT cat_id, category FROM category ORDER BY category";
	$result = mysqli_query($link, $sql);
	
	if (!$result){
		echo 'There was an error fetching categories: ' . mysqli_error($link);
		exit();
	}
	
	while ($row = mysqli_fetch_array($result)){
		$categories[] = array('catid' => $row['cat_id'], 'cat' => $row['category']);
	}
	
	if (isset($_REQUEST['catDDL'])){
		$catid = $_REQUEST['catDDL'];
		$sql = "SELECT subcat_id, subcategory FROM category WHERE cat_id = '$catid' ORDER BY category";
		$result = mysqli_query($link, $sql);
		
		if (!$result){
			echo 'There was an error fetching subcategories: ' . mysqli_error($link);
			exit();
		}
		
		while ($row = mysqli_fetch_array($result)){
			$subcategories[] = array('catid' => $row['subcat_id'], 'cat' => $row['subcategory']);
		}
	}
	
	include 'test.html.php';
?>



表格:text.html.php



The Form: text.html.php

<pre lang="PHP"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
	<title>Categories</title>
	<script type="text/javascript">
		function populateSubCategory(){
			window.location = ''index.php'';
		}
	</script>
</head>

<body>

	<form id="catform" action="index.php" method="post">
		Category:	
		<select id="catDDL" name="catDDL"  önchange="populateSubCategory();">
			<option value="">Select One<option
			<?php if(isset($categories)): ?><br mode="hold" /?>				<?php foreach($categories as $category): ??>
					<option value="<?php echo $category[''catid'']; ?>"?>
						<?php echo $category[''cat'']; ??>		
					</option>
				<?php endforeach; ??>
			<?php endif; ??>
		</select>
		<br />
		Sub Category:
		<select id="subCatDDL" name="subCatDDL">
			<option value="">Select sub category</option>
			<?php if(isset($subcategories)): ??>
				<?php foreach($subcategories as $subcategory): ??>
					<option value="<?php echo $subcategory[''catid'']; ?>"?>
						<?php echo $subcategory[''cat'']; ??>		
					</option>
				<?php endforeach; ??>
			<?php endif; ??>
		</select>
	</form>
	
</body>
</html>

推荐答案

link = mysqli_connect(' ' ' 如果(!
link = mysqli_connect('localhost', 'root', 'aweklin@topnetconsulting'); if (!


链接){ echo ' 连接到数据库服务器时出错:' . mysqli_error(
link){ echo 'There was an error connecting to database server: ' . mysqli_error(


链接); 出口(); } 如果(!mysqli_select_db(
link); exit(); } if (!mysqli_select_db(


这篇关于从另一个基于PHP的DropDownList自动填充DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆