I'm having this select, let's call it "X", that is populated with car brands as options from the database via a SELECT.
<select name="X">
<?php
$row = mysqli_query("SELECT * FROM brands");
while($row2 = mysqli_fetch_array($row))
echo '<option value="' . $row2['brandID'] . '">' . $row2['brandName'] . '</option>
?>
</select>
Now i have to populate the second select, called "Y", with the models specifics to a brand selected.
For example, if the option that's selected in the first select box (X) is Audi i should have as options in the second select (Y) the following: A4,A6,TT,TTs
To populate the second select box manually is easy, basicaly the same thing as for the first just with a different SQL request.
$row = mysqli_query("SELECT modelName from modele WHERE brandName = '$brand'");
Where $brand would have a value according to the first select's selection.
Thanks
Put an id in your <select name="X"> code like <select name="X" id ="X">
Put another select like <select name="Y" id="Y">. Which will be blank.
put this jquery in your page.
$("X").on("change",function(){
var x_value=$("X").val();
$.ajax({
url:'ajax.php',
data:{brand:x_value},
type: 'post',
success : function(resp){
$("#Y").html(resp);
},
error : function(resp){}
});
});
in your ajax.php add the query.
<?php
$row = mysqli_query("SELECT modelName from modele WHERE brandName =".$_POST['brand']);
while($row2 = mysqli_fetch_array($row))
echo '<option value="' . $row2['modelId'] . '">' . $row2['modelName'] . '</option>
?>
Hopefully it will work. Please tell me if you need anything.
Use ajax and on change event together,
method:
1- build a page where you post your queries to.
2- that page should react to the query and sends a respond with the desired list of options.
example :
if ($_POST["vehicle"] == car) // send response with array of car names as json obj for example
else // send response with array of motorbikes names
your ajax should receive it and populate the filed on-complete.
If you need more details, I'll be happy to provide it
Related
It is early in my PHP and HTML career. I want to populate a with a SQL query - no problem. But then, take the highlighted value from that query and use it to drive another query with which to populate a different drop down.
What I don't understand is how the values are passed between the HTML and the PHP, should I use a global variable to store the selection from the first drop-down? How do I do this?
Is there any alternative to using a button to indicate that the current highlighted value in the first drop-down is to be used to drive the query which populates the second drop-down?
My code looks like this:
Asset type:<br/>
<select size=3 class="element select medium" id="WantAssetCategory" name="WantAsset Class">
<?
$sql = "SELECT * from Categories";
$result = $dbcon->query($sql);
foreach ($result as $row){
$catname = $row['description'];
$catx = $row['id'];
echo '<option value = ' . "$catx> $catname</option>" ;
}
$result = null;
?>
</select>
<select size=3 class="element select medium" id="WantAssetCategory" name="WantAsset Class">
Use jQuery to listen to the change on the select and perform the action script with the selected value, in response update the options for the new drop down with the mentioned id.
document.getElementById("WantAssetCategory").onchange = function() {
$.ajax({
data: this.value, // This is the selected value from your drop down.
type: "POST", // GET or POST
url: "query.php", // the file(where the query is) to call
success: function(response) {
$('#output').html(response);// repose response should contain the new select options and #output should be the id of the new select drop down.
}
});
}
I hope that helps.
Its a bit hard to express what I want to do but let me give it a shot.
First of all I have a database with a table. One of the columns from that table I will be showing in a select dropdown menu in HTML, to be more precise: this outputs the column 'name' in the dropbown menu:
<?php
$query = mysql_query("SELECT name from table");
echo "<select name='select1'>";
while ($row = mysql_fetch_array($query))
{
echo "<option value'" . $row['name'] ."'>" . $row['name] . "</option>;
}
echo "</select>";
?>
Then when a value is selected in that dropdown, that value needs to be saved in a variable.
if (isset($_POST['button']))
{
$select = $_POST['select1'];
}
As last bit, the value in variable $select needs to be used to make a SELECT query. I want to use that variable in the SELECT statement to look for the corresponding row (in the same table) that is related to the value in the dropdown menu and pick a column and output that value to a textbox or checkbox, depending on as what the value needs to be outputted.
Example:
TABLE
id - name - street - number - ...
row 1: 0 - test1 - teststreet1 - 1 - ...
row 2: 1 - test2 - teststreet2 - 2 - ...
row 3: 2 - test3 - tesstreett3 - 3 - ...
1) I select test2 from the dropdown menu (dropdown menu is filled with the column name from database)
2) test2 is saved as a variable in $name
3) select query searches for value of street column where $name = test2
SELECT street from table where ?
row 2: 1 - test2 - tesstreett2 - 2 - ...
So I want teststreet2 from street to be outputted in a textbox
<input type='text' value='?'>
I need help with the select query or how to call this.
Thanks in advance and taking time to read this!
Kind Regards
Found it!:
if(isset($_POST['select1']))
{
$select = $_POST['select1'];
$street_select = mysql_query("SELECT street FROM table WHERE name = '" .&select. "'");
$street = mysql_fetch_array($street_select);
$resul_street = $street['street'];
}
Now I can do this for every value in each column that is asked via the dropdown menu!
You can not change a state of any loaded dropdown or text as it is.
You must use jQuery/Ajax here.
When you select on Dropdown value it will than trigger Ajax/jQuery and call another page where your select query will run and return value, which you can display in textbox using jQuery.
<select name = 'select1' onchange='return get_street()'>
<option name='test1'>test1</option>
<input type="text" name ="street" id="street" value="" />
<script>
function get_street()
{
var selected_val = $('option:selected', this).val();
$.ajax({ //ajax call
type: "POST", //method == POST
url: "street.php", //url to be called
data: "name="+selected_val, //data to be send
success: function(data){
$('#street').val(data); // here we will set a value of text box
}
});
}
</script>
And your street.php will look like
<?php
$name = $_POST['name'];
$res = mysql_query("select street from table where name = '".$name."'");
$row = mysql_fetch_assoc($res);
return $row['street']; // this will return a name of street
?>
Few things you need to make sure is
give appropriate path of your file in ajax
give id to your text input and same should be in ajax success
you must connect database in your street.php
I have a dropdown list of gender. I am getting the values from my table 'candidate' and in this table i have a field which is actually a foreign key to another table.
The field is gender_code_cde, and the table name is gender_code. Now gender_code contains 2 rows. and id and a description. Its structure is like:
1->Male
2->Female
Its being displayed in dropdown list. but I want to get the id 1 if male is selected and id 2 if female is selected. My code is below:
<p>
<label for ="gender">Gender:</label>
<?php
$query = mysql_query("select * from gender_code"); // Run your query
echo '<select name="GENDER">'; // Open your drop down box
//Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query))
{
echo '<option value="'.$row['gender_cde'].'">'.$row['gender_dsc'].'</option>';
}
echo '</select>';
?>
</p>
I am working in codeIgniter.
// this will alert value of dropdown whenever it will change
<script>
function getvalue(val)
{
alert(val);
}
</script>
<p>
<label for ="gender">Gender:</label>
<?php
$query = mysql_query("select * from gender_code"); // Run your query
echo '<select name="GENDER" id="Gender" onchange="getvalue(this.value)">'; // Open your drop down box
//Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query))
{
echo '<option value="'.$row['gender_cde'].'">'.$row['gender_dsc'].'</option>';
}
echo '</select>';
?>
</p>
In Javascript, you can get the value with native JS.
var e = document.getElementById("give-the-select-element-an-id");
var gender_cde = e.options[e.selectedIndex].value;
If, you want it back at the server, it will come in to PHP as $_GET['GENDER'] or $_POST['GENDER'] depending on if the form does a POST or a GET request.
If you are submitting a (POST) form and this is inside that form you can access the the value (id) from CodeIgniter's $_POST wrapper: $this->input->post('GENDER');. I don't use CodeIgniter so I could be wrong, but it will be located in your $_POST array from PHP.
If you just want to replicate the variable somewhere on the page you can just echo it. Or set via js/jQuery with document.getElementById('#something').value = value; or jQuery('#something').html(value);.
You should also change the following:
use a foreach in place of your while:
foreach ($query as $row) {
echo $row['gender_x'] . $row['gender_y'];
}
use the CodeIgniter SQL wrapper instead of depreciated PHP functions. The query part is $this->db->query('SELECT statement goes here');. You should look at your CodeIgniters' version documentation for a more in depth explanation.
EDIT: To make it a bit more clear, an example:
$query = $this->db->query('SELECT * FROM gender_code');
foreach ($query->result() as $row) {
echo '<option value="' . $row->gender_cde . '">' . $row->geneder_dsc . '</option>';
}
This is assuming you have setup the previous calls in CodeIgniter. Please see http://ellislab.com/codeigniter/user-guide/database/examples.html and you may wish to read http://ellislab.com/codeigniter/user-guide/
How to populate select box with data from mysql table, and then move that value from one page to another.
I did a small coding, and i was able to get all the values from table, but i cant move those value to other page.
$query1="SELECT * FROM seat_no WHERE seatno NOT IN(SELECT seatno FROM check_in_desk)";
$result1 = mysql_query($query1);
<select name="txt_seatno">
<?php
while($nt=mysql_fetch_array($result1))
{
echo "<option value=$nt[id]>$nt[seatno]</option>";
}
</select>
?>
Can you just lookup the values on the page where you need them? If not, you could pass the data as a GET or POST variable, and then use this to generate the select box options on the new page.
There's two ways I can think of;
1) append the value to the url, so it'd be;
echo '' . $nt[seatno] . '';
and in the receiving page, you'd put;
$id = (int) $_GET['id'];
2) Or you could simply use this;
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_select2
(dropdown menu)
(btw; your < /select> should come after ?>)
I have 2 tables, Provinces and Districts. I would like to populate a select field with options as District names based on which Province is chosen in another select. The Districts table has a ProvinceID field to reference which Province it belongs to. I know this is doable, I just can't figure it out. I also want to create and update the new Districts select without refreshing the page.
UPDATE: I'm writing it in PHP and MySQL, using jQuery as sparingly as possible.
In order to do it without AJAX, prepopulate a Javascript dataset... warning, if you have a lot of data this could be slow, but if it's a manageable list length, you could save some overhead from multiple AJAX requests loading the same data over and over.
var provinces = {};
provinces['province_a_id'] = [
{ name:'District A', id:'district_a_id' },
{ name:'District B', id:'district_b_id' }
];
provinces['province_b_id'] = [
{ name:'District C', id:'district_c_id' },
{ name:'District D', id:'district_d_id' }
];
function getDistricts( referenced_select ) {
var selected_province = $(referenced_select).val();
var district_select = $('#districts');
district_select.empty();
if ( provinces[selected_province] ) {
$.each( provinces[selected_province], function(i,v) {
district_select.append( $('<option value="' + v['id'] + '">').text( v['name'] ) );
} );
}
}
$(document).ready( function() {
$('#provinces').bind( 'change', function() {
getDistricts(this);
} );
} );
-- HTML
<select id="provinces" name="provinces">
<option value="province_a_id">Province A</option>
<option value="province_b_id">Province B</option>
</select>
<select id="districts" name="districts">
</select>
Make a php script and call it dp.php ( dp, short for data_provider, use any name you like). In dp.php
// get province id passed in via `post` or `get`
$pid = $_REQUEST['pid'];
// get the districts in that province
$query = "SELECT `district_id`, `district` FROM `districts` WHERE province_id` ='$pid'";
// link to your database
$link = mysqli_connect(HOST, USER, PASS, DBNAME);
// execute your query
$result = mysqli_query($link, $query);
// parse the options
while($row = mysqli_fetch_assoc($result)) {
$options .= '<option value="' . row['district_id'] . '">' . $row['district'] . "</option>\n";
}
// send options
echo $options
With the following markup in your page:
<select id="province" name="province">
<option value="ny">New York</option>
...
</select>
<select id="district" name="district">
</select>
Include the following jQuery:
// whenever a different province is selected
$('#province').change(function() {
// remove all options in district select
$('#district').html('');
// find the new province selected
var my_province = $('#province').val();
// get new options from server and put them in your district select
$('#district').get('path/to/dp.php?pid=' + my_province);
)};
You didn't state what server side technology you are using (if any). Here's an example in ASP.net - it should point you in the right direction:
http://www.mikesdotnetting.com/Article/97/Cascading-DropDownLists-with-jQuery-and-ASP.NET
I actually figured this out on my own using jQuery and post. On the primary select, I added onchange="getDistricts()" and used this for that function:
function getDistricts()
{
var province_id = $("#provinces").val();
$.post("handler.php",
{
"mode" : "get_districts",
"pid" : province_id
},
function(data)
{
$("#districts").html(data);
}, "text");
}
And then in handler.php, I have a case that catches the mode, and runs the following code:
<query on districts table>
while($row = $sql->fetchrow($result);
{
$id = $row['id'];
$name = $row['name'];
$html .= "<option value='$id' id='$id'>$name</option>";
}
echo $html;
I'm not a fan of this solution, and would really like something better, but it does what I need it to do for the moment. I hope this can help someone else out.