select drop down from db query based on previous selection (PHP/HTML) - php

i need to populate another drop down based on previous selected drop down. please help.My code is below. Now, how i pass the previous selected value to next dropdown where clause?
<td align="right">Country</td>
<td>
<select type="text" name="Country">
<option selected value="">Country Code</option>
<?php $result=mysql_query("SELECT CountryName FROM tbl_country");
while($row_result=mysql_fetch_row($result))
{ echo "<option value=\"$row_result[0]\">$row_result[1]</option>";}
?>
</select>*
</td>
<td align="right">Area</td>
<td>
<select type="text" name="Area">
<option selected value="">Area</option>
<?php $result=mysql_query("select Area from tbl_area where CountryName='???'");
while($row_result=mysql_fetch_row($result))
{ echo "<option value=\"$row_result[0]\">$row_result[0]</option>";}
?>
</select>*
</td>
How can i set the selected country name into the second queries where clause?
Thanks in advance.

As PHP is run on the server you will need to request the server to run the second query for you by either having the page refresh or do it via AJAX so that the page refreshes but without looking like it has.
The easiest solution would be to do it across 2 pages, if you want to do it on the one then AJAX is the way forward.
Give me a minute and i'll look for an example.

Related

Select value from the dropdown in which the data are taken from database

I need to post the value from the drop down, in which the dropdown contains items which are retrieved from the database. To display the item of the table I use echo in the option. But then, I need to get that value of item selected to be updated in the database. As be seen below, I've tried the code which (surely) will not work. How is it possible to get the the value of selected item?
Your suggestion will be appreciated.
Thank you.
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option name="user_name"><?php echo $hasil['user_name'] ?></option>
<option name="user_name" value="<?php echo $hasil['user_name'] ?>" hidden></option><?php }?>
</select>
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option><?php echo $hasil['user_name'] ?></option>
<?php } ?>
</select>
This code solves your problem, Check at your side and let me know in case of any issue.
We need to give name only once in select tag and no need to add a extra option only for value.
You must fill your dropdown from base table after that you must check value or id from second table that stored in database. If id or value is equal you must set selected='selected' in your option element
Hope it is help you to resolve you problem
" >
" >
" >
*

display result based on drop down list

I would like to display the result which I have selected based on the drop down list. This is my drop down list code which is retrieved from the finish_product under bom table (calculate.php)
<tr>
Select product:
<select class="itemTypes">
<?php
while ($row1 = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
</tr>
For example, under the drop down list I selected 'Table', how do I display the result in doCalculate.php?
Right now I just hard code Table which is not feasible. Anyone to help me out?
There are many, many ways which you could accomplish your goal. I would suggest using a JavaSctipt "onchange" event, detect the selected option, and then update the DOM with its contents.

dynamic drop down box from mysql

I got 3 input fields and each field are getting their data from its own tables called Tour type, country and destination respectively as shown
<label>Tour Type</label>
<select id="element_11" name="element_11" required>
<option value="" selected="selected">--Select--</option>
<?php
while($row=mysql_fetch_array($sql))
{
$tour_type_id=$row['tour_type_id'];
$name=$row['tour_name'];
echo "<option value='$tour_type_id'>$name</option>";
}
?>
</select>
<label>Country</label>
<select id="element_12" name="element_12" required>
<option value="" selected="selected">-- Select --</option>
<?php
$sql=mysql_query("Select countries_id,countries_name from countries");
while($row=mysql_fetch_array($sql))
{
$cid=$row['countries_id'];
$name=$row['countries_name'];
echo "<option value='$cid'>".$name."</option>";
}
?>
</select>
<label>Destination</label>
<select id="element_13" name="element_13" required>
<option value="" selected="selected">-- Select --</option>
<?php
$sql=mysql_query("Select destination_id,destination_name from destination");
while($row=mysql_fetch_array($sql))
{
$destination_id=$row['destination_id'];
$name=$row['destination_name'];
echo "<option value='$destination_id'>".$name."</option>";
}
?>
</select>
</div>
</li>
This is what i got as my 3 database tables i.e. tourtype, countries and destination respectively:
I am trying to make each field dependent on each other more like a dependent drop down box. For example if i select a tour type then the 2nd drop down should populate options only relevant to what is selected from the 1st drop down and so on. In this case for e.g if i select culture ,then the 2nd drop down should only show amsterdam and belgium.
Can anyone help me on this.
Please go through this link dependent dropdown using jquery ajax
How he maintained the relations among the entities
let me explain you if you want countries based on tour then you need to relate the country table with tour table as you have shown the country table in image it contains only two columns countries_id and countries_name you have to add one more column that is tour_type_id when you select any tour you will get the tour_type_id then your query should be
SELECT * FROM `countries` where `tour_type_id` = 1 //this is the id you will get from the tour_type select box
and this will populate the related countries same case for the destination related this table with country_id
Hope it makes sense
as i am an asp.net developer i can suggest you put your 2nd and 3rd drop down in an update panel(ajax update panels) then in your 2nd drop down query make it like $sql=mysql_query("Select countries_id,countries_name from countries where tour_type_id="+element_11.SelectedItem.Value+""); it will select those values whose tour_type_id is same as you selected in first drop down, same logic can be use in 3rd drop down as well
There are two ways to sole this.. you can use use php with a page refresh and 2nd way use jQuery ajax to populate fields accordingly.

refresh and re-populate select box

I populate my select box using php code below, my problem now is that i have my reference form which add new items to this select box. what I want is when I add new drawer item from my reference, i want to refresh this select box only not affecting the whole page. I read about ajax/jquery/js solution, but i don't know how to integrate it.
<td>Drawer</td>
<td>
<select name="municipality" class="country" style="width: 200px;">
<option selected="selected"></option>
<?php
$sql=mysql_query("select idDrawer,drawerName from drawer");
while($row=mysql_fetch_array($sql))
{
$id=$row['idDrawer'];
$data=$row['drawerName'];
echo '<option value="'.$id.'">'.$data.'</option>';
}
?>
</select>*
</td>
Your question leads to a broad discussion. I would suggest following these references:
http://www.youtube.com/watch?v=0CMTQtnZ0G0
http://www.youtube.com/watch?v=SbQUJdimia4
http://www.youtube.com/watch?v=GVHphoGi5dY

Can I pass Ajax var result to PHP code?

Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.
Greetings.
I am coding a basic form that uses a SELECT list I populate from my database.
However, my user needs the form to be dynamic and wants to be able to select either MasterTable1 or MasterTable2 or MasterTable3...
Instead of hardcoding the table name for the database query that populates the SELECT list, I attempted to implement a basic Ajax action (used example from w3schools)...and that's when I lost my sanity...
I can output <div id='txtHint'></div> in my page and it shows the correct table name that was picked.
But how do I pass the correct table name to my query that will populate my SELECT list???
I tried
<select name="DBFilename" id="DBFilename" size="0">
<option value="">Select Filename</option>
<?php
$sql="select distinct filename from "."<div id='txtHint'></div>";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option>
<?php } ?>
</select>
But to no avail. This is confusing since I can do this...
...
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gettable.php?q="+str,true);
xmlhttp.send();
}
</script></head>
<body><form><select name="SrcTbl" id="SrcTbl" size="0" onchange="showTable(this.value)">
<option value="">Select Data Table</option>
<option value=""> </option>
<option value="MasterTable1">up to 31 days old</option>
<option value="MasterTable2">62 days old</option>
</select>
</form><br /><div id="txtHint"><select name="tabList"><option></option></select> </div>
</body></html>
And the name of my table will be displayed in the SELECT list 'tablist'.
How do I pass the correct table name to my query that will populate my SELECT list? Thanks!!
(Pastebin =>form code)
m8, ajax is mainly used for user experience and populating a select list is so easy mode that it shouldnt be bothered with ajax in the first place!
You should use ajax if you want to use some methods on user submitted data and create an illusion of seamless data exchange between the server and the client, and based on the return results render a corresponding view or an element of the view.
unless you load every element of the view with ajax, you should populate your html with php from the start!
<select name="DBFilename" id="DBFilename" size="whatever since style belongs to css">
<option selected="selected">Select Filename</option>
<?php
$sql="SELECT DISTINCT filename from 'wherever'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename'];?>
</option>
<?php } ?>
</select>
Create a separate php script which returns a list of select options -> values depending on the table name given to it. You must remember to protect against sql injection. Then use ajax to retrieve the list and insert it into the select.

Categories