PHP Dropdownlist form with mysql - php

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.

Related

Implement onclick retrieve

I am new to PHP and I need help.
I have a MySQL Database table named CARS. It has columns BRAND, MODEL, TYPE. I want to show 2 select combo box. First combo box should retrieve the BRANDS from Database. And when user selects a BRAND then MODELS of that BRAND should be retrieved in the second select combobox.
Please help.
<select id="brands">
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1[0];?>"><?php echo $row1[0];?></option>
<?php endwhile;?>
</select>
I'm not sure what you want, but I'll try to understand it.
So, first Combo box should get all brands from DB and second one needs to show its models.
What you need to do:
You need to call javascript on-click function that will trigger query from database.
Also, you have to send value of selected brand as parameter of function just because you want to make a query with that parameter.
I'm providing you with a helpful link:
https://www.daniweb.com/programming/web-development/threads/356822/onclick-get-results-from-php

Changing shown values of table (MySQL)

Is it possible to get the values from a database-table column displayed?
For example as a html form table? or clickable button?
I have a table with values like lessonid, studentid, answer and corrected by student.
Now I can save different answer to the database, Show the selected lessons from the table.
But I want to "pick" one of them an "correct" it and save the selected answer to table.
What is the best way to solve this? Very grateful if someone have time to help me a bit.
Is it possible to get the values from a databasetable collumn shown for example as a html form table?
If you can't use an existing framework, the best way to do this is probably the use of a foreach loop.
echo "<table><tr><th>Table Headers :)</th></tr>";
foreach ($lesson as $unit) {
echo "<tr><td>".$unit['question']."</td></tr>";
}
echo "</table>";
Obviously, you could add many more columns in the same fashion.
But i want to "pick" one of them an "correct" it and save answer to table.
Not sure I understand the question, but you could potentially have a dropdown box in a form at the bottom of the page that lists the answers. You could select the "correct" one and click the submit button, which would post the data and write it to MySQL.
echo "<form action='' method='POST'>
<input type='hidden' name='action' value='answer'>
<select>
<option value='".$yourValueHere."'>".$yourLabelHere."</option>
...
</select></form>";
You could then have a section of the script that only executes when action == "answer". The section could write the selected answer to the MySQL database.

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

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.

PHP issues I have it outputting data from DB into a table, but then I need user input for an insert

I have a form page where users enter some data and upon hitting 'submit' a query is executed in our database that pulls up relevant information.
So, users put in two things into the form: class name and time.
after that, our php file pulls up a table with section information.
for example: ENG 212 has two sections: ENG212-001 and ENG212-101. So, in the table, it has information about the section like max enrollment and location, etc. it also shows how many people are currently enrolled in that section.
Now I want users to select a section. if the max enrollment is 10 and currently enrolled are 10 students, then the user can't select that. if currently enrolled is < max enrollment, then the user can select that section.
so, HTML page with form --> php file that pulls up the table ---> now i need a way for user to select one of the sections, but if they are full I want them not to be able to even select it.
I'm thinking of doing radio buttons but I have no idea how I'd implement them for this particular scenario
how do i implement this? i've tried looking online and I have no idea how to implement this.
any help would be appreciated.
After you submit the HTML Form and you get the max enrollment value check it in PHP and print the radio buttons from within PHP based on the value you got.
if ($max_of_section_1>10)
echo "<input type='radio' disabled='disabled' value='Section01'/>Section 01";
else
echo "<input type='radio' value='Section01'/>Section 01";
if ($max_of_section_2>10)
echo "<input type='radio' disabled='disabled' value='Section02'/>Section 02";
else
echo "<input type='radio' value='Section02'/>Section 02";
A simple if statement to evaluate if the class is full or not would do the trick.
if($row['status'] == '1') {
//Display code here
} else {
//Display class is full message here
}
If you are just trying to not allow an end user to select an input, try the html disabled attribute. if you determine a section to be full set your input method as disabled. you will still need a logical barrier in your back end too.

How do you populate a radio button or drop down box when editing from a database?

I know this is a similar question to one already asked, but I was hoping for a different answers to a problem. I have a form where you can upload new articles to a database all fine works ace, wonderful, brill. The form uses a drop down menu for the type of article, I have news, gossip, travel, performances and others in my drop down box. When you upload to the db it's only going to add the selected item let's say, in this case, News.
I need the user to be able edit their articles, so I need to populate the drop down box with the selected item. I think it's a bit of a pain in the BUM to set up a database table with each of the drop items and an ID for them. Is there away of setting the selected item as value in the db, and then just using a if statement or something to populate the rest of the drop down box?
Hope that makes sense. I also thought use of a radio buttons could be a solution however I cant get that to set the selected button with the value in the db.
You could do something like this:
// array of available options
$available = array('news'=>'News', 'gosip'=>'Gosip', 'travel'=>'Travel', 'performances'=>'Performance');
// selected option value
$selected = 'news';
echo '<select name="article-type">';
foreach ($available as $val => $label) {
echo '<option value="'.htmlspecialchars($val).'"';
if ($val == $selected) {
echo ' selected="selected"';
}
echo '>'.htmlspecialchars($label).'</option>';
}
echo '</select>';
Sounds like you want to put the article types in a table or something along those lines.
Then you can load the drop-down from the table, and just store a reference to the article type ID in the article table.
On the plus it should make retrieving all articles of a given type quite a bit faster as you'd be doing the lookup on ID.
It sounds to me like this is more a database design issue than anything else. Your basic design seems flawed. I'm trying to deduce the issue from your question so bear with me if I'm way off track.
Here is a relevant portion of the design, as it should be, IMO:
Table: Articles
ArticleID
ArticleContent
ArticleSubject (FK from Subjects)
..
..
.. Other columns.
Table: Subjects
SubjectID
SubjectDescription
..
..
.. Other columns.
Note that I called the second table Subjects to better illustrate my implication. This way, each Article has a reference to an article type (subject). Now when you retrieve data for the "Article types" dropdownlist, you would simply retrieve all the rows in the second table, and then would set the selected item based on the current selection (from the first table.)
$options = array(,);
$selection = filter_input(INPUT_POST, 'what');
$blah = true; //i don't know what to name this.
foreach($options as $option) {
echo '....';
if(isset($blah) && $option === $selection) {
echo 'selected="selected"';
unset($blah);
}
}
This is probably the fastest way. Also if you're going for speed make sure to take advantage of echo's multiple parameters.

Categories