post value one dropdown to other dropdown using php mysql - php

include('connection.php');
$con=mysql_query("select * from province"); ?>
<select name="pid" style="float:left; width:135px;">
<option value="">Select Country</option>
<?php while($row=mysql_fetch_array($con)) {
$pid=$row['province_id'];
?>
<option value="<?=$pid?>" ><?php echo $row['province_name'] ?>
</option>
<?php } ?>
</select> -
<?php
$pid=$_POST['$pid'];
include('connection.php');
$con=mysql_query("select * from city where city_state ='$pid' ") or die(mysql_error()); ?>
<select name="cid" style="float:left; width:135px;">
<option value="">Select City</option>
<?php while($row=mysql_fetch_array($con)) {
$cid=$row['city_id'];
?>
<option value="<?=$cid?>" ><?php echo $row['city_name'] ?> <?php }?>
</option>
</select>
first table is country table, that coutry id is province_id,
second table is city, that Province id is city_state,
so, post value one to another dropdown

For this, you could use AJAX. In your country dropdown box, just echo all the countries in <option> elements.
Then attach a change event on this dropdown box using jQuery, so that it will take the user's input (I mean selected country) and do an AJAX request(jQuery has various shorthand methods for AJAX too) to fetch all the provinces belonging to this country. So this request will be sent to a PHP page on your site, that will pull the details from your db using the country id that the user selected.
Then the result from the AJAX request, which would be list of provinces with their IDs is dynamically appended to the second dropdown box(ie. for province). And for this province dropdown box also you would attach a change event to it, and do the similar thing like I mentioned above to fetch all the cities under this province, using AJAX.
Do a quick research on AJAX, in Google. There are lots of tutorials to help you.

Related

PHP Grabbing Option ID from HTML Select

Currently setting up a form for users to select aircraft information from. Currently, the form allows users to select an airplane and in return when the form is submitted it passes the value"aircraft type" to an API. Due to this, I cannot set the values to be the registration of the airplanes so have put this into the ID for now (open to suggestions).
<select name="type" id="plane-list">
<option selected="selected">Choose one</option>
<?php
foreach($allaircraft as $item){
?>
<option id="<?php echo $item->registration; ?>" value="<?php echo $item->icao; ?>"><?php echo $item->name; ?> - <?php echo $item->registration; ?></option>
<?php
}
?>
</select>
As you can from above these are all dynamic values which change over time so hard coding the options are not possible.
So summed up I need to pull the ID or $item->registration; instead of ICAO code. This then needs to be set as a variable so that I can call a function with this code.
Let me know if you need me to clear up anything. Don't normally questions on here so be nice.

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.

how to store selected value of a drop down list in to a session variable in php

currently i am working on a system. here i have a drop down list and its values are populating from database. now what i need is when ever some one selects a record from the list the selected value should be get stored in a session variable which ll get displayed in an another page. can we do so. i am coding in php.
my code is:
<td align="center">
<?php
$sql_dep=" Select * from places_tbl";
$row_dep = $db->Execute($sql_dep);
$total_dep = $row_dep->RecordCount();
?>
<select name="place" class="txtbx08" id="place">
<option value="">--Please Select City--</option>
<?php if($total_dep>0) {
while(!$row_dep->EOF)
{
?>
<option value="<?php echo $row_dep->fields["place_id"];?>">
<?php echo ucfirst($row_dep->fields["place_name"]); ?>
</option>
<?php
$row_dep->MoveNext();
}}
?>
</select></td>
I'm not sure how you're retrieving the selected value (assumingly through POST), but the normal procedure would be:
session_start();
$_SESSION['place'] = $_POST['place'];

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.

how do i trigger the javascript event with dropdown and change the values?

I am creating a form where the users will have to select the country from the drop down list. as soon as they selects it should activate another drop down list where it should show the states belonging to the country and hence it should process the value from country Id of element. and as soon as the user selects the states another drop down list should activate listing the cities belonging to that particular states, and hence so on to areas.
for that i have written the php and html code like this.
Select Countries : <select name="countries"><br/>
<?php
foreach($property->getAllCountries() as $countries)
{
?>
<option value="<?php echo $countries['id']; ?>"><?php echo $countries['name']; ?></option>
<? } ?>
</select><br/>
Select State : <select name="state">
<?php
foreach($property->getStatesFromCountryId($countryId) as $states)
{
?>
<option value="<?php echo $states['id']; ?>"><?php echo $states['name']; ?></option>
<? } ?>
</select>
the javascript should automatically insert $countryId from and populate the states after the user selects it.
i am noob at javascript i would appreciate if someone could guide me how to achieve this?
thank you.
sorry for bad english.
you should add onchange="showstates()" to select countries and a div to add states of country to it
function showstates()
{
var country=document.getElemtById("countries").value;
if (country=="anywhere")
{
document.getElemtById("statesofcountries").innerHTML='<div>"states list"</div>';
}
}
check this link
You can always google it up:
http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html

Categories