Change the list of dropdown according to the selection of other dropdown - php

I have two dropdowns .i want second dropdown list shoul changed according to the value selected in first dropdown.
this is my first dropdown
Category :<select name="Category" id="a1_txtBox5" required="required">
<option value="select">select..</option>
<?php while($selectcategoryarray=mysql_fetch_array($selectcategory)) {
?>
<option value="<?php echo $selectcategoryarray[1];?>"><?php echo $selectcategoryarray[1];?></option>
<?php
}
?>
</select>
And here is my second dropdown:
<label style="margin-left:24px;">Subcategory :</label><select style="margin-right:35px;" name="subcategory" id="a1_txtBox3" required="required">
<option value="select"> select..</option>
<?php while($selectsubcategoryarray=mysql_fetch_array($selectsubcategory)) {
?>
<option value="<?php echo $selectsubcategoryarray[2];?>"><?php echo $selectsubcategoryarray[2];?></option>
<?php
}
?>
</select>
Please help.

Exactly you need to handle the Change Event for your first Select element and in the body of the event you need to send request to server for getting data of second Select element.
I recommend to use an ajax process to doing this.
And o this you should use jQuery for handling events and have ajax.

Related

Dynamic generate form based on select box change value

I want to generate dynamic form based on select box change value.
Suppose i have a parent select box :
<select name="select1" id="select1">
<option value="1">Fruit</option>
<option value="2">Animal</option>
<option value="3">Bird</option>
<option value="4">Car</option>
</select>
below select box is depend on first
<select name="select2" id="select2">
<option value="banana">Banana</option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
</select>
Here is my 2 select boxes and i want 2 things
1) create dependency of 2 this select box
2 ) I want to generate a dynamic form based on change second select box value.
for example if i select apple then a form open with related apple value.
I am a new in jquery and i do not have any idea about it ( share me code script if any one knows)
You can follow this way to making a dynamic dropdown.
The following HTML code contains dependent dropdowns for countries and states. Country options are read from the database and shown in the dropdown on page load. Initially, the state dropdown has no options. On changing the country dropdown values, a jQuery function is called to get dependent state options and loaded dynamically.
<div class="frmDronpDown">
<div class="row">
<label>Country:</label><br/>
<select name="country" id="country-list" class="demoInputBox" onChange="getState(this.value);">
<option value="">Select Country</option>
<?php
foreach($results as $country) {
?>
<option value="<?php echo $country["id"]; ?>"><?php echo $country["name"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>State:</label><br/>
<select name="state" id="state-list" class="demoInputBox">
<option value="">Select State</option>
</select>
</div>
</div>
The following jQuery script shows a function to send an AJAX request to PHP to read state list depends on the selected country. This AJAX call is set with the selected country id.
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "get_state.php",
data:'country_id='+val,
success: function(data){
$("#state-list").html(data);
}
});
}
</script>
In PHP, it connects the database to retrieve “states” table values based on the country id passed by jQuery AJAX call. It forms state dropdown options and returns as the AJAX response. This response is inserted to the state dropdown.
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["country_id"])) {
$query ="SELECT * FROM states WHERE countryID = '" . $_POST["country_id"] . "'";
$results = $db_handle->runQuery($query);
?>
<option value="">Select State</option>
<?php
foreach($results as $state) {
?>
<option value="<?php echo $state["id"]; ?>"><?php echo $state["name"]; ?></option>
<?php
}
}
?>

Whats the possibility of one updating a database field with a form having a drop down without changing the value of the drop down field

Lets say i input data into a mysql database using a drop down like:
<div>
<input name="name" type="text" id="name" placeholder="Enter Name"></div>
<div>
<select name="position" id="position" type="text">
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
With a normal text field, i can simply include the value field to show on the form the initially entered data in the table. And change that so when i submit form, it updates the related field.
<div>
<select name="position" id="position" type="text" value="<?php echo $row_position['Position']; ?>>
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
So assuming i do not want to update the field relating to the drop down, how do i it? Because what happens is, once i submit the form without selecting any option from the drop down, it picks the first option from the list which is the "Select" option and uses that to update the related field in the database.
I'd suggest at least two steps to make it user-friendly and achieve your goals:
First, make the initially entered selection selected if available:
<?php
$positions = Array("A", "B", "C");
?>
<div>
<select name="position" id="position">
<option value="-1">Select</option>
<?php
foreach($positions as $v) {
$selected = ($row_position['Position']===$v) ? "selected" : "";
echo "<option value='$v' $selected>$v</option>";
}
?>
</select>
</div>
And on the receiving php-script check if you've received a valid option, otherwise send error-message:
<?php
//other stuff...
if($_POST['position']>0) {
// save that value
} else {
// send error to user
}
// even more stuff..
?>
Notes: in select tag there is no type=text nor a value=anything available.
Assuming you are fetching dropdown options from database.
Note: Array keys, variables are only for demonstration purposes. This may differ from your actual records.
<div>
<select name="position" id="position">
<?php foreach($options as $option): ?>
<option value="<?= $option['position'] ?>" <?php if($option['position'] == $current_record['position']) { echo "selected"; } ?>> <!-- e.g. $option['id'] -->
<?= $option['name'] ?>
</option>
<? endforeach; ?>
</select>
</div>
What i have done that has solved the issue was to simply add the value from the database to the selectedoption or the first option field. So by default without changing the drop down, the first option with the value from the database is selected. And so there would be no change.
<div>
<div><?php echo $row_position['Position']; ?></div>
<select name="position" id="position">
<option value="<?php echo $row_position['Position']; ?>Select to Change</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
This gives a simple or straight forward solution to the problem.
Will still try the other suggestions to see how it all plays out. Thanks guys.

Change textbox value using dropdown selected in php and mysql

I have texbox and dropdown list which is populated from mysql database.I want to change textbox value using dropdown list, without refreshing the page.
Here is my code and Thanks in Advance.
<select name="select" id="dropdownlist1">
<option id="0">-- Select the Company --</option>
<?php
require("dbcon.php");
$getallcompanies = mysql_query("SELECT * FROM ifcandetails6");
while($viewallcompanies = mysql_fetch_array($getallcompanies)){
?>
<option id="<?php echo $viewallcompanies['tcuid']; ?>"><?php echo $viewallcompanies['tcname'] ?></option>
<?php
}
?>
</select>
Here is my Input field code:
<input type="text" id="field1" value="<?php echo $viewallcompanies['tcname']?>" disabled/>
Use following
$(document).ready(function(){
$("#dropdownlist1").change(function(){
$("#field1").val($(this).val());
});
});
As you can do it on front side itself, you dont need to change in your PHP code. Add the following code on DOM ready.

GET the value of the selected option using php then show it with echo

I'm Beginner on PHP, Javascript, I would like to get selected value from select/option and stock it into a php variable ( $varpeoplesnames ), to use it in a future SQL request and show it with echo
my code:
<select name="peoplesnames">
<option value="1">john</option>
<option value="2">sarah</option>
<option value="3">samantha</option>
</select>
<?php (....) echo $varpeoplesnames;
How can I get the text of the selected option to show it with echo in php?
Wrap it in a form with action being the link to your php file and a submit button
HTML:
<form action='yourPhpFile.php' method='GET'>
<select name="peoplesnames">
<option value="1">john</option>
<option value="2">sarah</option>
<option value="3">samantha</option>
</select>
<input type='submit'></input>
</form>
PHP
<?php
$varpeoplesnames = $_GET['peoplesnames'];
echo $varpeoplesnames;
?>

Is there a way in PHP to get the index of a currently selected HTML <option>?

I'm trying to use PHP to get the current index of my dropdown box.
<select name="iface" id="iface">
<option>vlan10</option>
<option>br0</option>
<option>br1</option>
<option>eth3</option>
</select>
The options are set from a loop that grabs interfaces from a server. I need to get the currently selected index if that is possible.
<form method="post">
<select name="iface" id="iface">
<option>vlan10</option>
<option>br0</option>
<option>br1</option>
<option>eth3</option>
</select>
<input type="submit" value="Print selected option"/>
</form>
<?php
if (isset($_POST['iface']))
{
echo $_POST['iface'];
}
?>
For example, the selected interface is "eth3".
So, just render with php:
<option selected="selected">eth3</option>
for indexing you render your list like this:
<select name="iface" id="iface">
<option value="0">vlan10</option>
<option value="1">br0</option>
<option value="2">br1</option>
<option value="3" selected="selected">eth3</option>
</select>
Now iface.value (or $_POST['iface']) = 3

Categories