Solved

jQuery fill selectbox from database

Posted on 2010-08-15
17
688 Views
Last Modified: 2012-05-10
Hey guys,

well I'm having some problems with the following code.
I'm trying to fill a selectbox with data from the database.
I've got this so far
	$("input#artistname").change(function() { 
		UpdateDropDown($(this).val());
    });

    function UpdateDropDown(artist) {
		
		jQuery.post('get-albums.php', 
		{parameterSentToServer1:'value', param2:'value2'}, 
		function(data){jQuery('select#select_albumname option').remove(); //Remove current options
		
		for (var option in data.results){
			jQuery('select#select_albumname').append('<option value="'+option.value+'">'+option.name+'</option>');
		}}, 'json');
    }

Open in new window


In the .php file I've given a static result
echo '{results:[{value:1, name:"album1"}]}';

So I think it should work, but it's not working. What am I doin wrong here?
0
Comment
Question by:Serellyn
  • 10
  • 7
17 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Not sure you return value and name keys+values from your php function :

value and name

{"results":[{"value":"","name":"Choose one"},{"value":"1","name":"Exodus"},{"value":"2","name":"Uprising"}]}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Try this for testing purpose :


<?PHP
	echo json_encode(array(results=>array(array(value=>"",name=>"Choose one"), array(value=>"1",name=>"Exodus"), array(value=>"2",name=>"Uprising"))));
?>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
If it don't work, check this working code with the previous php code.

<!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 language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$("#getalbum").click(function() {
			UpdateDropDown( $("#artist").val() );
		});
	});
	function UpdateDropDown(artist) {
		jQuery.post('get-albums.php',
			{ "parameterSentToServer1":"value", "param2":"value2" },
			function(data) {
				jQuery('select#select_albumname option').remove();
				jQuery.map(data.results, function(option) {
					jQuery('select#select_albumname').append('<option value="'+option.value+'">'+option.name+'</option>');
				})
			},
			'json'
		);
	}
</script>
</head>
<body>
<input id="artist" type="text" readonly="readonly" value="Bob Marley" /><input id="getalbum"  type="button" value="get Albums" /><br />
<select id="select_albumname"><option value="test">test</option></select>
</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:Serellyn
Comment Utility
Hello Leakim,

with your second snippit the selectbox gets filled, but it fills up with UNDEFINED (4 times)
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
which one ? #33442272 ?
0
 
LVL 1

Author Comment

by:Serellyn
Comment Utility
No, 33442255, it returns UNDEFINED (4 times) in the selectbox.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
OK, use the call function
function(data) {
				jQuery('select#select_albumname option').remove();
				jQuery.map(data.results, function(option) {
					jQuery('select#select_albumname').append('<option value="'+option.value+'">'+option.name+'</option>');
				})
			}

Open in new window

0
 
LVL 1

Author Closing Comment

by:Serellyn
Comment Utility
And now it works like a charm, thank you
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
You're welcome! Thanks for the points!
0
 
LVL 1

Author Comment

by:Serellyn
Comment Utility
Hey leakim, just one more question, you might know and I really need it.
I now need to get values from the databse.
So I've got this

// QUERY IS HERE

$j = array();
while ($row = mysql_fetch_array($get_albums_sql))
{
       //array needs to be created here
}

So how can I now create that array that you've created staticly for me?
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
something like :


$j = array();
while ($row = mysql_fetch_array($get_albums_sql)) 
{
      $j[] = array(value=>$row[0],name=>$row[1]);
}
echo json_encode(array("results"=>$j));

Open in new window

0
 
LVL 1

Author Comment

by:Serellyn
Comment Utility
You sir, are a genius!
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
;-)
0
 
LVL 1

Author Comment

by:Serellyn
Comment Utility
Something stupid though, first I did this, and it did select everything from nightwish.
$artistname = 'nightwish';

Now I'm doing $artistname = trim($_POST['artistname']);
And when I now enter nightwish, nothing happens...
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
from your code you send : { "parameterSentToServer1":"value", "param2":"value2" },

no artistname
0
 
LVL 1

Author Comment

by:Serellyn
Comment Utility
I'd figure you would like some points, you deserve them since you've helped me again.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_26404631.html
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
^_^ I 'm going to check your account twice now! Thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now