We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,485 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to load Radio button with value from Database using JQUERY

Hello There,

Please Help, I have a form that generates data from database. i have to select value from a combobox so that i can see the value. but i wanted that data from database to be the value of the radio button. is that possible?I haven't started yet? can somebody give me a hint or a code snippet? i really need your help!Thanks!


-cigoL

2
Contributors
6
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
cigoL..:)
Light Poster
44 posts since Jul 2011
Reputation Points: 9
Solved Threads: 9
Skill Endorsements: 2

THat's difficult to follow. Could you explain that again?

1. you get data from DB based on what a form sends - OK
2. select value from combobox - ok - but what value are you talking about?
3. you want DB data to be the value of the radiobutton. What radio button??

diafol
Keep Smiling
Moderator
10,675 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

The Values from Combobox is
i.e Array -> ('a',b','c','d');
now if i selected 'a'
it will run a query searching for 'a'
i.e "SELECT * from type_tb WHERE type ='a'"

then in radio button you will get the result from query
i.e
radio button1 value is 'ARMS'
radio button2 value is 'AIM'

well i can do it in simple PHP but i have it refresh it again, i wanted to do it dynamically . i'm new to jQuery since i'm stuck with other programming language for a long time ..

cigoL..:)
Light Poster
44 posts since Jul 2011
Reputation Points: 9
Solved Threads: 9
Skill Endorsements: 2

Thanks for your help i already figure this out thank you ! i'll mark it as solved!:)

cigoL..:)
Light Poster
44 posts since Jul 2011
Reputation Points: 9
Solved Threads: 9
Skill Endorsements: 2
Question Answered as of 1 Year Ago by diafol

OK, that's relatively easy. Mind you, you could use 'false ajax' (my terminology) if you only have a handful of data items in the whole unfiltered dataset. That's when on page load, you get all the records from the SELECT * from type_tb type query and place the data into a js array. When you then select from the dropdown, it runs a js script to get the 'a' array items or the 'c' array items.

Just a thought. It saves going to the server and making DB server calls every time somebody selects something from the dropdown.

The down side of this is that any changes to the DB in the meantime will not be displayed on dropdown select. If your data is not going to be updated much, this should not be a great issue.

ANy use?

diafol
Keep Smiling
Moderator
10,675 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

As I posted my comment before it was solved (simultaneous post with your last one), I thought I should include my idea:

$listarray[] = 'Choose a letter...';
$r = mysql_query("SELECT SUBSTRING(`words`,1,1) AS letter, GROUP_CONCAT(`words`) AS `output` FROM table GROUP BY letter");
while($d=mysql_fetch_assoc($r)){
	$x = explode(',',$d['output']);
	sort($x);
	$t[$d['letter']] = $x;
	$listarray[] = $d['letter'];
}
$myarray = json_encode($t);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
	var x = <?php echo $myarray;?>;
	function showMe(){
		var i = me.value;
		var str = '';
		if(x[i].length > 0){
			for(z = 0;z < x[i].length; z++){
				(z === 0) ? chk =  ' checked="checked"' : chk = "";
				str =  str + '<input id="' + x[i][z] + '" type="radio" name="help" ' + chk + ' /> <label for="' + x[i][z] + '">' + x[i][z] + '</label>';	
			}
		}
		document.getElementById('showradios').innerHTML = str;
	}
</script>
</head>
<body>
<form>
	<select id="me" onchange="showMe();">
    	<?php
			foreach($listarray as $item){
				echo "<option value=\"$item\">$item</option>";	
			}
		?>
    </select>

<div id="showradios">

</div>
...
</form>
</body>
</html>

Somebody may find it useful or may even point out how to improve it.

diafol
Keep Smiling
Moderator
10,675 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

Thank you sir, i got a hint from your "false ajax" terminology i use simple javascript. actually i make it very simple and easy to understand, for us "newbies", sir thank you for your time and code :)

cigoL..:)
Light Poster
44 posts since Jul 2011
Reputation Points: 9
Solved Threads: 9
Skill Endorsements: 2

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0729 seconds using 2.69MB