Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 695
  • Last Modified:

jQuery fill selectbox from database

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
Serellyn
Asked:
Serellyn
  • 10
  • 7
1 Solution
 
leakim971PluritechnicianCommented:
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
 
leakim971PluritechnicianCommented:
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
 
leakim971PluritechnicianCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SerellynAuthor Commented:
Hello Leakim,

with your second snippit the selectbox gets filled, but it fills up with UNDEFINED (4 times)
0
 
leakim971PluritechnicianCommented:
which one ? #33442272 ?
0
 
SerellynAuthor Commented:
No, 33442255, it returns UNDEFINED (4 times) in the selectbox.
0
 
leakim971PluritechnicianCommented:
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
 
SerellynAuthor Commented:
And now it works like a charm, thank you
0
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!
0
 
SerellynAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
 
SerellynAuthor Commented:
You sir, are a genius!
0
 
leakim971PluritechnicianCommented:
;-)
0
 
SerellynAuthor Commented:
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
 
leakim971PluritechnicianCommented:
from your code you send : { "parameterSentToServer1":"value", "param2":"value2" },

no artistname
0
 
SerellynAuthor Commented:
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
 
leakim971PluritechnicianCommented:
^_^ I 'm going to check your account twice now! Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now