Trying to populate second dropdown menu from first dropdown menu php mysql - php

I am trying to populate 3 menus, 1st menu is created from mysql query and php and displays TVshows ( ie. Modern Family, Dexter, etc ), what I would like to do is once the TVShow is selected populate the next drop down with a new mysql query for seasons ( 1 ,2 ,3 etc.) , then populate a 3rd drop down via mysql query based off the first 2 options being selected for episode
The table is as follows
| id | Title | Season | Episode | Extension | URL
I can get the first drop down to display with the following code
<?php
$sql="Select distinct title from TVShows";
$result=mysql_query($sql);
echo "<select name='TVShow'><option value=''>Select TV Show</option>";
while($row = mysql_fetch_array($result))
{
echo "<option value=$row[title]>$row[title]</option>";
}
echo "</select>";
?>
I have tried many examples but none seem to work right, I would like to be able to do this on the same page as opposed to having the user click submit to go to another page to select the second drop down.
I would like code to dynamically setup the 2nd dropdown based on the first choice, then dynamically setup the 3rd dropdown based on the 1st and second dropdowns

mysql_fetch_array does not fetch an associative array.
Try using mysql_fetch_assoc instead

For better performance on the user end you might consider loading all results from the db (and then ideally caching them) and then hiding and showing the appropriate ones with javascript.
This way if users are clicking on your menu a lot you're not making a ton of unnecessary round trips to the db.

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 Dropdownlist form with mysql

Hey, i am about to make a form which should contain 4 dropdownlists... And by dropdownlists i mean selects in html/php - the "preview" of what i am thinking about, there's a link in the bottom, so you can get a better understanding of what i am talking about.
I've been searching for some, and i found this code in a tutorial, it is working and all this, but i would like to have 4 dropdownlists instead of only 2. I have seen some without querystring, but this code is with querystring, and i'm not quite the best at php, but i understand a lot, thats not a problem, but problem is, that i would like to get rid of querystring and get 2 more dropdownlist, but i don't know the way to do it.
it's not a problem to get 2 more dropdownlist, but to get 2 more dropdownlist working like the link in the bottom.
I want it to work so i have to choose the first dropdownlist and rest are disabled or just empty, and when i choose in the first dropdownlist the second dropdownlist will be enabled or filled with data which is connected with the first like, a car and in the next BMW etc. should show up, and then when you have choosed in the second dropdownlist, the third dropdownlist will be enabled or filled with data just like the second one, but now if you choose BMW you should pick a model like M5 or M3 etc. and when you have done that, you will be able to pick the engine of the M5 or M3 or whatever you are choosing.
///////// Getting the data from Mysql table for first list box//////////
$quer2="SELECT DISTINCT category,cat_id FROM category order by category";
///////////// End of query for first list box////////////
/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($cat) and strlen($cat) > 0){
$quer="SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory";
}else{$quer="SELECT DISTINCT subcategory FROM subcategory order by subcategory"; }
////////// end of query for second subcategory drop down list box ///////////////////////////
echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example action=dd-check.php////
////////// Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
foreach ($dbo->query($quer2) as $noticia2) {
if($noticia2['cat_id']==#$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
////////////////// This will end the first drop down list ///////////
////////// Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
foreach ($dbo->query($quer) as $noticia) {
echo "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";
////////////////// This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>
Like i mentioned earlier, i have been searching for some results, and then i also found this. This is what i am talking about, this is without querystring, and is doing exactly what i would like to.
I have tried looking into the code (F12) but can't find some php or javascript or jquery.
http://www.sedox-performance.com/tfconfigurator/dealers?lang=en&color=blue&bg=white&txt=black&title=blue
Quite a few methods available. You want a dropdown's options to be filtered based on the previous dropdown selection, right? If lists are not too long then build and send them down with the page-load, then filter child options on parent's select event. Else if the lists are very long then use AJAX to pull child lists on parent's select event. Use JSON to transport and / or manage the lists and JQuery (or straight JavaScript) to dynamically populate and repopulate the lists.

Inserting database output into specific div tags

I have a form set up, and I want the user to be able to enter information and then select via radio buttons which div the database will output the inserted data into. Right now I have it set up to where the user can input his information, and the information will be output into a div, but i'm trying to figure out how I would go about inserting data from the 1 database into different divs. For example:
The user inserts: Hello World
and it is input in to the database and displayed in div1, but then later the user decides to input "How are you?" and that be displayed in div2 and you would have something that looks like
| div1 | | div2 |
| Hello World | | How are you? |
Do I need to create 2 separate tables within my database that correspond with my respective div tags? What is the simplest way to go about this?
Use like this:
<?php
$result = mysql_query("SELECT * FROM table where column='column'");
while($row = mysql_fetch_array($result))
{
if($row['id']=="Hello World")
{
echo "<div id='div1'>".$$row['id']."</div>";
}
else if($row['id']=="How are you?")
{
echo "<div id='div2'>".$$row['id']."</div>";
}
}
?>
There are many ways to do this.
Perhaps the most logical and correct way is to have one table with the following columns: user_id, div_name, and text
Another way would be to have one table and a user_id, div1, and div2 table
As you mentioned, you could have two tables both with the structure user_id, text where the determination of which div it is for is in the table name but this is not good form and I'd suggest against it.

reactivating a query after executing an insert statement in PHP/MYSQL

I have 2 forms. The first form the user would select a department (DEPQ) from a drop down menu list. Once that is done they would submit that form and the results would be displayed in a drop down menu in the second form. The user would then use this second form to insert data into a table in msql database using the derived values. What I am trying to do is once the system has completed the insertion of the data, the system automatically re populates the drop down menu with the results derived from first query. I know this can be simply achieved with multiple linked drop down menu's but I have been unable to find a suitable tutorial for this function. Any help you all can give me would be greatly appreciated.
Well you have to implement any opportunity to identify the user. Maybe a user system, maybe only a cookie/localstorage or something else. Important is, that the user can by identified with an unique id.
In the next step you create a mysql table where you save the first selection of the user may be in this format
id | user_id | department
-------------------------
1 | 3 | depA
2 | 15 | depB
Then when the user visits your page again, you identifie the user and populate the select element.

Showing selected country in a dropdown using PHP

On my page I have a dropdown menu with a list of countries, this is a page for users to update their address details so I would like to have the "selected" value when they visit the page set to what the entered upon signup.
I have
<option value="254" >Afghanistan</option><option value="255" >Albania</option>
Etc as my HTML markup and in the database the user's country is stored as the code like in the markup.
Normally this would be simple but as it is such a vast list of countries I can't write php code in each one manually.
Thanks
You tagged the question as MySQL.
The answer is there:
Make a table called countries
Table countries
---------------
id integer auto_increment
name varchar(255)
code char(3)
....
And run a query like
SELECT name FROM countries ORDER BY name
Use the output in your dropdown.
If you are using PHP to create the drop-down, simply insert the "select" test into the loop. (If you aren't, then you might want to - create an array of country vals or get from DB and create the options with a PHP loop.)

Categories