Update dependent dropdown not showing specific data - php

I am trying to create a UPDATE page which contain dependent dropdown.
When the 1st dropdown is selected, the dependent dropdown does not show data for the specific data selected in the 1st dropdown.
It is showing "Select Option".
Below is what i have try so far.
1st dropdown
> <label>DEPARTMENT</label>
<div class="col-sm-10">
<select name="dept_ID" class="form-control">
<option value="">Select Department</option>
<option value="1" <?php if($data['dept_ID'] == '1'){ echo 'selected'; } ?>>Department One</option>
<option value="6" <?php if($data['dept_ID'] == '6'){ echo 'selected'; } ?>>Department Six</option>
<option value="2" <?php if($data['dept_ID'] == '2'){ echo 'selected'; } ?>>Department Two</option>
<option value="3" <?php if($data['dept_ID'] == '3'){ echo 'selected'; } ?>>Department Three</option>
</select>
</div>
>
Dependent dropdown
<label>SUPERVISOR : </label>
<div class="col-sm-10">
<select class="form-control" name="op_supervisor" id="selectID">
<option>Select Option</option>
<?php $sql = "SELECT * FROM supervisor JOIN DEPARTMENT ON supervisor.dept_ID = DEPARTMENT.dept_ID ";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)) {?>
<option value="<?php echo $row['op_supervisor'] ?>"> <?php echo $row['dept_name'] ?> - <?php echo $row['op_supervisor'] ?></option>
<?php }?>
</select>
</div>
My expected result is the dependent dropdown will show specific data chosen in the department dropdown and what have been selected before.

Related

Populate a dropdown from mysql and have the chosen item display in the dropdown after page refreshes

I am using php with a dropdown to select data and display the selected item in the dropdown after the page refreshes so have used this code which works.
<select name="select" id="mysort" onchange="select(this.value);">
<option value="5" <?php if($selecting == '5'):?> selected="selected"<?php endif;?>>5 </option>
<option value="6" <?php if($selecting == '6'):?> selected="selected"<?php endif;?>>6 </option>
<option value="7" <?php if($selecting == '7'):?> selected="selected"<?php endif;?>>7 </option>
<option value="8" <?php if($selecting == '8'):?> selected="selected"<?php endif;?>>8 </option>
</select>
I then want to be able to populate the dropdown from a mysql database. This code works:
$sql = "SELECT DISTINCT number FROM files ORDER BY number";
$result = $mysqli->query($sql);
echo '<select name="choose" id="mychoice">';
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<option value='.$row["number"].'>'.$row,["number"].'</option>';
}
echo '</select>';
}
I am having trouble with the syntax when I put the two blocks of code together so that the values are populated from the mysql and the selected option displays in the dropdown after the page refreshes.
I would be grateful for any help with this.
I think you want this -
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row["number"].'"';
if($selecting == $row["number"]) { echo " selected "; }
echo '>'.$row["number"].'</option>';
}

How to add option fetched from the database to the end in select php

I have a categories table where I have different categories. I have also add Other in the category. When I'm getting the categories to populate the select I want the other option to appear at last. how can I do that?
<select name="category" id="category" class="form-control <?php if (isset($errors['category'])) echo 'form-error'; ?>">
<option value="">Select a category...</option>
<?php while ($category = mysqli_fetch_assoc($categories)) { ?>
<option value="<?php echo h($category['id']); ?>" <?php if (isset($_POST['category']) && h($category['id']) === $_POST['category']) echo 'selected'; ?>><?php echo h($category['name']); ?></option>
<?php } ?>
</select>
If the Id of Other category is fixed, then you have to ignore it in your database query.
Let's say that the Id of that option is 1, our query will be like this:
select * from categories where Id <> 1
This query brings all categories except "other".
After that do the following:
<select name="category" id="category" class="form-control <?php if (isset($errors['category'])) echo 'form-error'; ?>">
<option value="">Select a category...</option>
<?php while ($category =
mysqli_fetch_assoc($categories)) { ?>
<option value="<?php echo h($category['id']); ?>" <?php
if (isset($_POST['category']) && h($category['id']) ===
$_POST['category']) echo 'selected'; ?>><?php echo
h($category['name']); ?></option>
<?php } ?>
<option value="1">Other</option>
</select>
hope it helps

how can i use 'selected' atribute for a <select> in a while loop?

<label for="brand_id">`Select Brand Name :` </label>
<select name="filter[brand_id]">
<option value=""></option>
<?php
if(isset($_GET['filter']['brand_id']) || (isset($_GET['filter']['category_id'])))
{
$selected = 'selected';
}else
{
$selected = '';
}
while($brandsRow = $resultBrandsRow->`fetch_assoc())`
{
echo '<option value="'.$brandsRow['id'].'" '.$selected.'>'.$brandsRow['brand_name'].'</option>';
}
?>
</select>
<label for="category_id">`Select Category Name :` </label>
<select name="filter[category_id]">
<option value=""></option>
<?php
while($categoriesRow = $resultCategoriesRow->`fetch_assoc())`
{
echo '<option value="'.$categoriesRow['id'].'" '.$selected.'>`'.$categoriesRow['category_name'].'`</option>';
}
?>
</select>
<input type="submit" value=">>"/>`
After clicking submit, the selected option should be selected. In the preceding example, after option is selected, every time submit is clicked, show the last record from my list, not the chosen option.
A simple example would be this case:
<?php
$a = 1;
?>
<select>
<option value='so and so' <?= ($a === 1) ? 'selected' : ''; ?>>
</select>

How do I keep the drop down value selected when I update the record/form data in PHP?

Here is the code that i am using for drop down to select.
<select name="formreg" value="">
<option value="Registered" <?php if($f_reg == 'Registered') { ?> selected <?php echo $f_reg; } ?> >Registered</option>
<option value="NonReg" <?php if($f_reg == 'NonReg') { ?> selected <?php echo $f_reg; } ?> >NonReg</option>
</select>
<select name="regform">
<option value="Registered" <?php if (isset($register) && $register=='Registered') {?> selected='selected' <?php echo $register; }?>>Registered</option>
<option value="NonReg" <?php if (isset($nonreg) && $nonreg == 'NonReg') {?> selected='selected' <?php echo $nonreg;} ?>>NonReg</option>
</select>
At the top of script:
$f_reg = $_POST["f_reg"]; // or $f_reg = $_GET["f_reg"] if you pass parameters as the url part
then your < select > tag:
<select name="formreg">
<option value="Registered" <?php if ($f_reg == "Registered") print "selected"?>>Registered</option>
<option value="NonReg" <?php if ($f_reg == "NonReg") print "selected"?>>NonReg</option>
</select>

PHP while in edit mode show selected value in to drop down

This question was asked already, but my question is very simple.
In the my account page, I have the employee country in a dropdown.
How to select a value in the combo, when in edit mode?
Let's assume you have the user's country in $user_country and the list of all countries in $all_countries array:
<select id="country">
<?php
foreach ( $all_countries as $country ):
$selected = "";
if ( $country == $user_country )
$selected = "selected";
?>
<option value="<?php echo $country; ?>"
selected="<?php echo $selected; ?>">
<?php echo $country; ?>
</option>
<?php
endforeach; ?>
</select>
should work.
An option tag will be the default for a select list when the selected attribute is set. In the following code option 2 will show up as the current selected option when the page loads:
<select>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
To achieve this in your PHP code conditionally display the selected attribute on your options against what the current value is:
<option value="1"<?php if($user['country'] == '1') { ?> selected="selected"<?php } ?>>1</option>
<option value="2"<?php if($user['country'] == '2') { ?> selected="selected"<?php } ?>>2</option>
<option value="3"<?php if($user['country'] == '3') { ?> selected="selected"<?php } ?>>3</option>
function p_edit_combo($cCurstatus,$h_code_default,$h_name=NULL){
<select name="<?php echo $cCurstatus;?>" id="<?php echo $cCurstatus;?>" class="main_form_select">
<option value="">Select</option>
<?php
$sql_h = "SELECT h_code,h_name FROM med_hl WHERE status = 1";
$sql_h_result = mysql_query($sql_h);
while($row=mysql_fetch_array($sql_h_result)){
$h_code = $row['h_code'];
$h_name = $row['h_name'];
?>
<option <?php if($h_code_default==$h_code){ ?> selected="selected" <?php }?> value='<?php echo $h_code; ?>' >
<?php echo $h_code."|".$h_name; ?>
</option>
<?php } ?>
</select>
<?php
}
**i have two table
" users" colmns(fname,lname,...as on ohther_infomation,hobbies datatype(int))
"info" columns (id (primary_key),hobbies(varchar 200)); in which i stored for hobbies name
In my case i am storing values in from (1,2,3,4) in hobbies (int) filled of users table which i matached them through join after time of fetch them,
in my info table i stored hobbies by their name (reading, writing,playing,gyming)
$row has our users selected hobbies (int)
$rows has list of our hobbies(varchar)
edit.php i need Dropdown value selected :==== And i am Doing Like this :--- (100% Working)**
<div class="form-control">
<label for="hobbies">Hobbies</label>
<select name="hobbies">
<?php
$query = "SELECT * FROM info";
$results = mysqli_query($connect, $query);
while ($rows = mysqli_fetch_array($results)) {
?>
<option <?php if ($rows['id'] == $row['hobbies']) { ?> selected="selected" <?php } ?> value='<?php echo $rows['id']; ?>'>
<?php echo $rows['hobbies']; ?>
</option>
<?php
}
?>
</select>
<span class="text-danger"><?php if (isset($err_hobbies)) echo $err_hobbies; ?></span>
</div>

Categories