Solved

jQuery fill selectbox from database

Posted on 2010-08-15
17
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
17 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33442254
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
ID: 33442255
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
ID: 33442272
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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 1

Author Comment

by:Serellyn
ID: 33442274
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
ID: 33442285
which one ? #33442272 ?
0
 
LVL 1

Author Comment

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

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33442306
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
ID: 33442314
And now it works like a charm, thank you
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33442322
You're welcome! Thanks for the points!
0
 
LVL 1

Author Comment

by:Serellyn
ID: 33442412
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
ID: 33442435
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
ID: 33442439
You sir, are a genius!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33442450
;-)
0
 
LVL 1

Author Comment

by:Serellyn
ID: 33442465
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
ID: 33442493
from your code you send : { "parameterSentToServer1":"value", "param2":"value2" },

no artistname
0
 
LVL 1

Author Comment

by:Serellyn
ID: 33442531
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
ID: 33442543
^_^ I 'm going to check your account twice now! Thanks
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…
Suggested Courses

627 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