I want to create a dynamic dropdown in loop - php

My requirement is to display CSV headers as the first element, then for each header, I want to map the element to elements appearing in drop-down. When first drop-down element is selected, it should dynamically populate second drop-down.
Example say my csv has the headers: first, second, third my first drop-down has values: 1, 2, 3 Then Based on 1: I should get second drop-down as a, b Then Based on 2: i should get second drop-down as i, j Then Based on 3: I should get second drop-down as p, q
i.e. First: (drop-down if 1 selected) (drop-down a, b) appears.
My problem is I am getting this done when I do not run this on loop. When I give a for loop for populating the csv header, automatically, the send drop-down does not appear.
Any help would be appreciated!

Related

Number of dropdowns based on selection of first dropdown

Programming in Bootstrap and PHP I am trying to create a dynamic form which has the following.
User uses a dropdown to select number from 1 to 10.
The page then displays x number of dropdowns.
Each dropdown is the same and displays a list of userID from a MySQL table tblUsers
Once the user makes a selection from each dropdown, it then shows a second dropdown immediately underneath showing a list of videos from a second MySQL table tblVideos. Meaning that the second dropdown is based on the results from the first.
Once all dropdowns have been selected. Submit is pressed and the results are sent as an array to the table tblVideoStored
Any ideas how the PHP and MySQL would be in a simple form..
Based on what you plan to achieve, you may want to look into combining php with ajax (Javascipt). Below are some examples that should set you on the right path
https://www.sitepoint.com/community/t/using-ajax-to-populate-dropdown-menu/4350/2
http://blog.chapagain.com.np/using-jquery-ajax-populate-selection-list/

PHP- Get index/associated value of option selected in dropdown box

I want to show values in a dropdown box (either from an array or database - please advise on which option is method)
Then
when a user selects a value in dropdown box, i want to have and get its associated index value (like 1, 2).
For example: dropdown box shows values:
"Car"
"Bicycle"
If user selects "Car", when i get dropdown selected value, i should get 1, similarly for "Bicycle" i get 2 .. and so on.
Please advise easy and simplest method to implement this
Thanks
You can use two methods:
1) Set up prior to display a code that holds associative array with key -> value i.e: 1 => Car (you can keep it in config file if it doesn't change frequently, you can pull it from database or you can keep it in some other form: serialized, file etc.) and use it when the submitted form is being processed.
2) Use array with key and value with the same string i.e: Car => Car and when the form is processed you will have value right away. This solution has some limitations and be troublesome with using more words or other characters that need to be sanitized.
I would advise option 1, you will have to set up the list before and use it after form has been processed but it allows more freedom and its less maintenance.

PHP selection in dropdown list

I need to create 4 dropdown list where the options are the names of the candidates (ex. 5 candidates) in a certain position (ex.Representatives). When i select a candidate from the list in the first dropdown, there will be 4 options in the second dropdown, and in the third dropdown there will be 3 options and so on. To sum it up, what is the php code for removing the list in the next dropdown upon selection without submitting it yet.
Use jQuery......jQuery help you to do validations easily. So it may take less time for client side validation
For e.g.;
$("#id_of_dropdown").change(function () {
// Some Code Here like you want to change another dropdown put if else here
});

Second select load from database after first one is selected

I have 2 select items.
In the first one I'm loading the options from a MySQL database table using PHP.
The second one I want to load the fields from another table where for example category field equals the value of the item selected on the first tag.
I have no idea about how I can make the second select tag load the options dynamically. I know I need to use Ajax I guess.
I've found this, but it doesn't help too much since the options of the first select tag depend on what it can find in the database.
// Given the options in the first dropdown are: "foo", "bar", and "baz"
var secondData = {
foo: ['lorem', 'ipsum'],
bar: [1,2,3],
baz: []
}
Then I need to add a 'change' event to the first dropdown, and given the value of that dropdown, load the contents of the second dropdown with the values contained in the secondData object, but I can't apply this.
If you want to get the values for the second dropdown from a database, then you do need ajax.
See this post:
Populate dropdown 2 based on selection in dropdown 1
A couple of other good posts re the basics of AJAX:
A simple example
More complicated example

two dropdownlist

I have two dropdownlist boxes. If I click on the first dropdown box, then the values have been changed in the second listbox accordingly to first listbox.
For example:
One is Fruits,vegetables,Birds. If i click on the Fruits then it shows the fruit list items in the second list box. It is same for other list also.
Hai Praveen,
Have a look at these examples for question reguarding cascading dropdown
http://www.phpclasses.org/browse/package/2053.html
http://www.skillfusion.com/articles/ajaxDropdown.php
http://codestips.com/php-ajax-cascading-dropdown-using-mysql/
http://www.daniweb.com/forums/thread132703.html
http://www.weberdev.com/get_example-4505.html

Categories