Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

populate select from other select value using jquery

hi all I have seen many question but no one has fulfilled my thirst. I want to populate select on the basis of one other select. I am using zend framework. I have a select that will select a account and on the basis of that selected id i want to populate other select with values fetched from database based on first selected option. I have first select which is like this

<select name='select1'>
<option value='1'>accoutn1</option>
<option value='2'>accoutn2</option>
<option value='3'>accoutn3</option>
<option value='4'>accoutn4</option>
</select>

Now what I want that on the basis of first select, the other other select is populated using jquery. The options will be values fetched from database on the basis of first selected value. my other select is

<select name='client'>
</select>

Which function of jquery supports this that I can fetched values from database on the basisi of selected option and populate other select. I am using zendframe work. Any idea?

like image 200
Awais Qarni Avatar asked Nov 28 '25 15:11

Awais Qarni


2 Answers

If you are using jQuery you can use this plugin

Demonstration and usage shown here

like image 116
Michal Jajcaj Avatar answered Dec 01 '25 04:12

Michal Jajcaj


Using events you can bind the first select. Instead of name, use ID or a class.

Using a $.get() you could dynamically get info from a php page. Make that php Page echo out the < option > tags you need for the second drop down.

Bind this into an onclick event to get the currently selected value, OR use a selector to get it:

var SelectedVal = $('#option1').val();
var Content;

$.get('GetValues.php?SelectedValue=' + SelectedVal , function(data) {
 Content = data;
});

Then using $('#client').append(Content); you can fill the select. Append will add "Content" inside the tag.

Hope this gives you a good starting point.

like image 41
Henry Avatar answered Dec 01 '25 06:12

Henry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!