I have two tables one tblEmployee and another tblCity.
The two tables are setup like this:
tblEmployee
-------------
employeeID
cityID
name
sex
contactNo
tblCity
-------------
cityID
cityName
I would like to create a form whereby an admin can update an employee's details. The form has a drop down box which is populated by the cityNames in tblCity.
I am struggling coming up with a query to allow the admin to select one of these cities from the drop down which will update the employee's cityID to the corresponding city in tblCity.
Many thanks in advance!
I suspect what you want is to setup the select values like this:
<option value="cityID">cityName</option>
The cityID will be submitted, and then you put that value in the tblEmployee.cityID.
For example:
<p>
<select id="sel1">
<option value="001">Auburn</option>
<option value="002">Austin</option>
<option value="003" selected>Dallas</option>
<option value="004">Houston</option>
</select>
<button rel="sel1">Show selected value (with value attribute)</button>
</p>
<p>
<select id="sel2">
<option>Auburn</option>
<option>Austin</option>
<option selected>Dallas</option>
<option>Houston</option>
</select>
<button rel="sel2">Show selected value (no value attribute)</button>
</p>
$('button').click(function(){
var $sel = $('#' + $(this).attr('rel')).find('option:selected');
console.log($sel.val() + ' - ' + $sel.text());
});
http://jsfiddle.net/5McA9/1/
Related
I'm making a website in HTML and PHP.
I want to make a dropdown menu using select and also have an input field as well.
Basically I have a mysql database.
Say a table in it is called Project, with columns ID, Name, Budget, Start_date.
I want a dropdown menu that allows me to select the columns: ID, Name, Budget, Start_date.
After the column is selected, I want to be able to input a value (also might be nice if I can specify number vs text for each different column for input type)
<form action="somepage.php" method="GET">
ID: <input type="text" name="Pid" /><br><br>
And then on somepage.php, I will be doing this:
$myvariable = $_GET['Pid'];
$sql = "INSERT INTO Project(ID)
VALUES('$myvariable' ";
I know how to do all this with multiple input fields with one submit button.
But I want to know how to do this with SELECT dropdown.
Is there some way to tell on the somepage.php what dropdown element was selected?
Because I need to know which column in Table Project that $myvar is supposed to be inserted into.
Thanks!
You can check by name if something was selected in select. Same as input text. I'm giving you an example here, not mentioning that you need to work on security against SQL injection, that is some other topic.
<form action="somepage.php" method="GET">
<select name="first">
<option value="-1" selected>Select something...</select>
<option value="first_chice">First choice</select>
</select>
<select name="second">
<option value="-1" selected>Select something else ...</select>
<option value="first_chice">Other First choice</select>
</select>
</form>
<?php
//somepage.php
if(isset($_GET['first']) && ($_GET['first'] != "-1")) {
//You have a selection in first select box
echo $_GET['first'];
}
if(isset($_GET['second']) && ($_GET['second'] != "-1")) {
//You have a selection in first select box
echo $_GET['second'];
}
?>
I have php search filter page in which i used different fields..One of them is Age of youngest driver field which have 2 values like 21-24 and 25+ ..Basically i only want to keep the selected values in the select box even after search so user know what he searched for ..What happen now when i select 25+ from the field and click on search it goes back to 21-24 . But As page is connected top database so it picks values from database which i enter like the database screenshot below Please check and let me know if it have any solution
<select class="half" id="form_frame" name="" onsubmit="getData(this);"/>
<option id="value1" value="21-24" selected="selected">21-24</option>
<option id="value2" value="25+">25+</option>
</select>
When i slect all values from the field and selct 25+ from driver age field then url is
http://localhost/Vehicle2/?car_type=0&pickup=0&return=0&residence_two=International&driver_age=25%2B&passengers=No.+of+Passengers&search=
http://localhost/Vehicle2/?car_type=0&pickup=0&return=0&residence_two=International&driver_age=21-24&passengers=No.+of+Passengers&search=
I want driverage=21-24 instead of driver_age
Apply onchange event in javascript like as follows if you are using jquery
jQuery(function () {
jQuery("#form_frame").change(function () {
var id = jQuery(this).val();;
location.href = "http://localhost/php-page/option"+id+"="+id
})
})
Simple use change event handler for selectbox
$(document).on('change','#form_frame',function(){
//window.location.href="http://localhost/php-//page/option"+$(this).//val()+"="$(this).val();
console.log("http://localhost/phppage/option"+$(this).val()+"="+$(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="half" id="form_frame" name="">
<option value="10" selected="selected">10</option>
<option value="11">11</option>
</select>
I'm working on a hotel booking project (php) and I want to create a booking-form where I will have :
Checking Date(datepicker)
Checkout Date(datepicker)
Room type (Here I want to have a dropdown menu, <select><option>Single</option></select>, and when a option is selected to show me the price of that room type, without pressing any button.)
Price (this is a text, not a textbox)(And here I want to show me the price of that room type.)
I have the query that gives me the price data from table, put I don't know how to show me without pressing any button, just when I select the "Single Room" option, the price field should be $30 (this is data from table, for example).
Can you give me an idea ? Is a specific function for <select> tag for this ?
<?php
if(isset($_POST['submit'])){
$tip = isset($_POST['roomtype']) ? mysql_real_escape_string($_POST['roomtype']) : '';
echo $tip;
}
?>
This is my php code, but its work only if I hit 'submit'. I want to work when I select the option.
Use the following example
<select class="text_select" id="field_6" name="field_6">
<option value="- Select -">- Select -</option>
<option value="Option One">Option One</option>
<option value="Option Two">Option Two</option>
<option value="Option Three">Option Three</option>
</select>
<label class="form_field">Your selected <span id="aggregator_name"></span>?</label>
and jquery
function notEmpty(){
var e = document.getElementById("field_6");
var strUser = e.options[e.selectedIndex].value;
document.getElementById('aggregator_name').innerHTML = strUser;
}
notEmpty()
document.getElementById("field_6").onchange = notEmpty;
jsfiddle link http://jsfiddle.net/Mj4Vj/
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.
I want to Create HTML Dynamic Combobox which populate using php scripting from db and onselection change values of another dynamic Combobox. Initially 2nd combobox should be invisible and on selection of 1st combobox make 2nd combobox visible related with similar data. for example, I have this table :
Category Name
Airport ABC
Airport XYZ
College a1
College b1
College b2
busstop a
busstop b
busstop c
busstop d
So, 1st Combobox will contain Unique Category listing (like: Airport, College, busStop)
and on the base of this selection enable 2nd combobox with related values like if user selected Airport then 2nd combobox will contain only (ABC, XYZ).
I basically want to do this with only HTML,JAVASCRIPT AND PHP only.
any suggestions are appreciated..
With the following snippet I make the assumption you have an array filled with your database rows as objects, which I will name $results;
edit: How to get your query objects:
http://www.php.net/manual/en/mysqli-result.fetch-object.php
I start with gathering the data for creating the comboboxes:
$combobox_data = array();
$results = mysqli_query("SELECT * FROM TABLE");
//create a multi dimensional array with names per category
while($row = mysqli_fetch_object($results)){
$combobox_data[$row->Category][] = $row->Name;
}
$category_combobox_html = "";
$name_combo_boxes_html = "";
//create category combo_box
foreach($combobox_data as $category=>$names){
//Add category option to category combo-box
$category_combobox_html .= '<option value="'.$category.'">'.$category.'</option>';
//Create Names combo-box for this category
$name_combo_boxes_html .= '<select id="'.$category.'" name="'.$category.'" class="hidden_combobox">';
//loop names, to add Names in the combo-box for this category
foreach($names as $name){
$name_combo_boxes_html .= '<option value="'.$name.'">'.$name.'</option>';
}
//end your combo box for this category
$name_combo_boxes_html .= '</select>';
}
your css
<style type="text/css" media="screen">
.hidden_combobox{
display:none;
}
</style>
your html
<select name="categories" id="categories">
<?php echo $category_combobox_html; ?>
</select>
<?php echo name_combo_boxes_html ;?>
your javascript
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
//when you select something from category box
$("#categories").change(function(){
//get selected category
var selectedValue = $(this).find(":selected").val();
//hide all nameboxes
$('.namebox').each(function(){
$(this).hide();
});
//show combobox for this select
$('#'+selectedValue).show();
});
</script>
Your result will be this:
All name comboboxes will be hidden unless you select a category which matches the combo_box
<select name="categories" id="categories">
<option value="Airport">Airport</option>
<option value="College">College</option>
<option value="busstop">busstop</option>
</select>
<select id="Airport" name="Airport" class="namesbox hidden_combobox">
<option value="ABC">ABC</option>
<option value="XYZ">XYZ</option>
</select>
<select id="College" name="College" class="namesbox hidden_combobox">
<option value="a1">a1</option>
<option value="b1">b1</option>
<option value="b2">b2</option>
</select>
<select id="busstop" name="busstop" class="namesbox hidden_combobox">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
</select>
Have you tried Google-ing it?
It seems that you're looking for something like this.