如何根据选择的单选按钮从数据库填充选择框 [英] How do you populate a select box from a database based on a choice of a radio button

查看:84
本文介绍了如何根据选择的单选按钮从数据库填充选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜遍过,今天找不到合适的答案。这似乎是一个非常普通的需求。



我在php的页面上工作,它有一个3个相互的单选按钮。当您单击其中一个时,我需要使用MS SQL Server数据库中的一组不同数据填充选择框。



我知道如何从中获取数据数据库。我知道如何设置表单项。我试过jquery,普通的javascript,其他随机的想法。我没有做任何工作,甚至没有意义。是的我知道服务器端和客户端之间的区别。



县列表略有不同,具体取决于用户选择的单选按钮。显然我不想在此时发布表格。用户应该填写一些其他选项,包括从多选框中选择县。



单选按钮和选择在同一表格上其他一些选项,比如在点击提交之前选择用户必须填写的日期。



我完全陷入困境。我所有的在线搜索都提出了其他问题的解决方案,这些问题有些相关但不完全相关。我知道如何根据单击的单选按钮更改javascript中的选择框的值,但是我必须从数据库中获取各个县,而不是将它们硬编码到php文件中。



我想也许获取数据的PHP代码必须来自一个单独的php文件,并且使用ajax调用它但我不确定我是否在正确的轨道上。



我尝试了什么:



所以我有3个单选按钮:



I've searched all over and can't find the right answer for this today. It seems like a pretty ordinary need.

I'm working on a page in php that has a 3 mutual radio buttons. When you click one of them I need to populate a select box with a different set of data from an MS SQL Server database.

I know how to get the data from the database. I know how to set up the form items. I've tried jquery, ordinary javascript, other random ideas. Nothing I'm doing works or even makes sense. Yes I know the difference between server side and client side.

The list of counties is slightly different depending on which radio button the user chooses. Obviously I don't want to post the form at this point. The user is supposed to fill in some other options including make a selection of the counties from the multiselect box.

The radio buttons and the select are on the same form with some other options like choosing dates that the user has to fill in before hitting submit.

I'm totally stuck. All my online searching comes up with solutions to other problems that are slightly related but not quite there. I know how to change the values of a select box in javascript based on which radio button is clicked, but I have to get the counties from the database, not hard code them into the php file.

I'm thinking maybe the php code that gets the data has to come from a separate php file and it's called using ajax but I'm not sure I'm on the right track.

What I have tried:

So I've got my 3 radio buttons:

<td>
	<input type="radio" name="in_out" id="in_out" value="I"> In County<br>
	<input type="radio" name="in_out" id="in_out" value="O" checked> Out County<br>
	<input type="radio" name="in_out" id="in_out" value="B"> Both<br>
</td>





这是我填写选择框的PHP代码:





Here's my php code to populate the select box:

<select name="county_list" multiple size="10" style="width:120px" >
<?php
	
	$in_out = 'B';

 	if ($in_out == 'I') {
		$db_query = "SELECT County FROM dbo.tblCounties WHERE InOut = 'I' AND DummyCounty = 0 ORDER BY County DESC";
	} else if ($in_out == 'O') {
		$db_query = "SELECT County FROM dbo.tblCounties WHERE InOut = 'O' AND DummyCounty = 0 ORDER BY County DESC";
	} else {
		$db_query = "SELECT County FROM dbo.tblCounties WHERE DummyCounty = 0 ORDER BY County DESC";
	}
																
	try {
	
		$stmt = $pdo->prepare($db_query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
		$stmt->execute();
		$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST);

		do {
			$county = $row[0];
			echo '<option value="' . $county . '">' . $county . '</option>';									
		} while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR));					

		$stmt = null;

	} catch (PDOException $e) {
		print $e->getMessage();
	}

?>			
</select> 

推荐答案

in_out ='B';

if(
in_out = 'B'; if (


in_out =='I'){
in_out == 'I') {


db_query =SELECT County FROM dbo.tblCounties WHERE InOut ='I'和DummyCounty = 0 ORDER BY County DESC;
}否则if(
db_query = "SELECT County FROM dbo.tblCounties WHERE InOut = 'I' AND DummyCounty = 0 ORDER BY County DESC"; } else if (


这篇关于如何根据选择的单选按钮从数据库填充选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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