How to populate a child drop down menu from parent selection PHP - php

How would one populate a child drop down selection on a static page from a parent drop down using only PHP. E.g having a child drop down populate with counties after you have selected which state you live in, or in my case populate series after manufacturer.
EDIT: Is there a way to do it without js?
<?php
$man = mysqli_query($con,"SELECT DISTINCT manufacturer FROM inventory.manufacturer WHERE manufacturer!=\"\" ORDER BY manufacturer;");
echo "<select name=\"manufacturerS\">
<option value=\"\">Select</option>";
while($row = mysqli_fetch_array($man)) {
echo "<option value=\"".$row['manufacturer']."\">".$row['manufacturer']."</option>";
}
echo "</td>
<td>";
if(isset($_POST['manufacturerS'])){
$ser = mysqli_query($con,"SELECT DISTINCT series, manufacturer FROM inventory.audit WHERE series!=\"\" AND manufacturer='".$_POST['manufacturerS']."' ORDER BY series;");
echo "<select name=\"seriesS\">
<option value=\"\">Select</option>";
while($row = mysqli_fetch_array($ser)) {
echo "<option value=\"".$row['series']."\">".$row['series']."</option>";
}
echo "</td>
<td>";
}

You need of client-side to determine when manufacturerS select was selected, then you can use pure javascript:
<form id="manufacturerF" method="POST">
<select name="manufacturerS" onchange="document.getElementById('manufacturerF').submit();">
<?php
$man = mysqli_query($con, "SELECT DISTINCT manufacturer
FROM inventory.manufacturer
WHERE manufacturer!=\"\"
ORDER BY manufacturer;");
while ($row = mysqli_fetch_array($man)) {
echo '<option value="'.$row['manufacturer'].'>'.$row['manufacturer'].'</option>';
}
?>
</select>
</form>
<?php
if(isset($_POST['manufacturerS']) && !empty($_POST['manufacturerS'])){
echo '<select name="seriesS">';
$ser = mysqli_query($con,"SELECT DISTINCT series, manufacturer
FROM inventory.audit
WHERE series!=\"\"
AND manufacturer='".$_POST['manufacturerS']."'
ORDER BY series;");
while ($row = mysqli_fetch_array($ser)) {
echo '<option value="'.$row['series'].'">'.$row['series'].'</option>';
}
echo '</select>';
}
?>
I recommend you use JQuery so will not need a <form> and reload your page.
The correct idea would be to use JQUERY to detect when the select1 was selected and then with AJAX populate the select2

Related

How to add an additional option to an option form which is filled with a db query

How can I add an extra <option> with a value of all to:
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());
$query = "SELECT name,id,path FROM categories ORDER BY ID DESC LIMIT 0,6";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="categories">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['path']."'>'".$row['name']."'</option>";
}
?>
</select>
I would be grateful for any suggestions
Edit
echo "<th>Tierart";
$query = $pdo->query("select sp_term from species");
//Abfrage der Tabelle Tierart
echo '<select name="sp_term">';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="all">all</option>';
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';
Since you're populating the values of option in a select tag section with a database query doing something as follows would do the trick.
echo '<select name="sp_term">';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';
However, when you want to add an additional item like All as an option, place an explicit option value before the while/after the while depending on your requirement and you'll have the select > option with the new All option added to the options from the database.
echo '<select name="sp_term">';
echo '<option value="all">all</option>';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';

PHP/MySQL: Values in a select box associate to values in associative array

I have a dropdown list (HTML select box) which gets values from this MySQL query:
"SELECT cdID, cdTitle FROM CD ORDER BY cdID"
The result is then stored in an associative array, which is then output to the dropdown list:
<?php
echo '<select name= "list" id="list">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['cdTitle'].'">'.$row['cdTitle'].'</option>';
}
echo '</select>';
?>
My issue is that I would like the user to see the title of the CD, but for the actual value to be "cdID" as that is the foreign key used in my database.
Just change the attribute echoed out for value -
<?php
echo '<select name= "list" id="list">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['cdID'].'">'.$row['cdTitle'].'</option>';
}
echo '</select>';
?>
why not set the option value with the cdID.
echo '<option value="{$row['cdID']}">{$row['cdTitle']}</option>';
or
echo sprintf('<option value="%s">%s</option>',$row['cdID'],$row['cdTitle']);

How to get selected value from dropdown list after submitting form in php

I would like to remain my drop down value which I select for submitting after posting the form. My form posts to the same page.
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
echo "Country: <select name='country' value=''>";
while($r = mysql_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id.">".$cname."</option>";
}
echo "</select>"; ?>
Remove your current echo inside the loop and replace it with the following:
if($_POST["country"]==$id)
echo "<option value='".$id."' selected='selected'>".$cname."</option>";
else
echo "<option value='".$id."' >".$cname."</option>";
This will check if the current option being displayed is the one that was submitted and it will select it in that case.
If I understand what you are looking for correctly you need to use the $_POST value of your select to set the selected item...
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
$country = '';
echo "Country: <select name='country'>";
while($r = mysqli_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id;
echo ($_POST["country"]==$id) ? ' selected="SELECTED"' : '';
echo ">".$cname."</option>";
}
echo "</select>"; ?>
Setting selected="SELECTED" for the $id that matches $_POST['country'] will make it the selected item in your dropdown.
And, get rid of mysql* functions and use mysqli* functions instead...

Populate multiple drop-down lists with a single query

I have four drop-down lists that I would like to populate with values from an MSSQL table. All four lists should contain the same values. The query looks like this:
$data = $con->prepare("SELECT ID, Code FROM Table WHERE Code = :value ORDER BY Code");
$input = array('value'=>'value'); //'value' is hardcoded, not a variable
$data->execute($input);
And here is the code for my drop-downs:
<?php
echo "<select name=\"proj1[]\">";
while($row = $data->fetch(PDO::FETCH_BOTH))
{
echo "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
echo "</select>";
?>
This works fine for one drop-down. If I try to create another one (proj2[], proj3[], proj4[]) and apply the same query, however, the PHP page stops loading at that point and the second drop-down does not populate. The only way I've found around it is to copy the query and change the variables ($data becomes $data2 for proj2[], and so on). I'd really rather not have to write the same query four times. Is there a way around it?
$select = '';
while($row = $data->fetch(PDO::FETCH_BOTH))
{
$select .= "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
echo "<select name=\"proj1[]\">";
echo $select;
echo "</select>";
echo "<select name=\"proj2[]\">";
echo $select;
echo "</select>";
//etc...
Why not just put all of it in a veriable and then using it 4 times?
Somthing like this:
<?php
while($row = $data->fetch(PDO::FETCH_BOTH))
{
$options .= "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
for($i = 0; $i <= 4; $i++){
echo "<select name=\"proj1[]\">";
echo $options;
echo "</select>";
}
?>

How to prevent echoing same value twice in php

So I have this drop down list in my form which pull "tags" from database as value for drop down options:
<select name="cartags">
<?php $result = mysql_query("SELECT * FROM Products WHERE ID > '0'");
while($row = mysql_fetch_array($result))
{
echo "<option value=\""; echo $row['Tag']; echo "\""; echo ">"; echo $row['Tag']; echo "</option>";
}
?>
</select>
What is my problem? My problem is that I am adding a lot of products into my databas and my code make dropdown list with tags for all this producst even if they have same tag. So what I need is solution how to prevent that same tag appear twice in my drop down.
I am pretty new to PHP and this is my first question here so I really hope that I explained my problem well.
Thanks in advance!
What is the purpose of WHERE ID > '0'? If ID is an auto-increment then it will always be positive. If not, it should be.
Why are you using mysql_fetch_array and then only using the associative keys? You should use mysql_fetch_assoc instead.
Why are you using a new echo every time you want to output a variable? Just concatenate.
Why are you setting the same string in value as the option's text? Without a value, it defaults to the text anyway.
Why are you not using backticks around your column and table names?
Try this instead:
<select name="cartags">
<?php
$result = mysql_query("SELECT DISTINCT `Tag` FROM `Products`");
while(list($tag) = mysql_fetch_row($result)) {
echo "<option>".$tag."</option>";
}
?>
</select>
Try this
<select name="cartags">
<?php $result = mysql_query("SELECT Tag, COUNT(Tag) tg Products WHERE ID > '0' GROUP BY Tag HAVING COUNT(Tag)>0 ORDER BY tg DESC");
while($row = mysql_fetch_array($result))
{
echo "<option value=\""; echo $row['tg']; echo "\""; echo ">"; echo $row['tg']; echo " </option>";
}
?>
</select>
It will also display the top tags that have the most first.

Categories