Set the default value on a select option drop down - php

I want to make the default value the same as set in the database. This is because i am using it to edit items in the databse and want the values to be preset.
This is currently setting it to black at the start.
$_SESSION["menuID"] = $_POST["menuID"];
$menuID = $_POST["menuID"];
$menu_sql = "SELECT * FROM menu WHERE menuID = $menuID";
$menu_query = mysqli_query($dbconnect, $menu_sql);
$menu_aa = mysqli_fetch_assoc($menu_query);
$select_sql = "SELECT * FROM course ORDER BY courseID";
$select_query = mysqli_query($dbconnect, $select_sql);
$select_aa = mysqli_fetch_assoc($select_query);
<select name="courseID">
<?php do { ?>
<option selected="<?php echo $select_aa['courseID']; ?>" value="<?php echo $select_aa['courseID']; ?>"><?php echo $select_aa['name']; ?></option>
<?php } while ($select_aa = mysqli_fetch_assoc($select_query)); ?>
</select>

Setting default using ternary operator.
<select name="courseID">
<?php do { ?>
<option selected="<?php echo ( $select_aa['courseID'] == 'Your default value here' ? "selected" : "" ); ?>" value="<?php echo $select_aa['courseID']; ?>"><?php echo $select_aa['name']; ?></option>
<?php } while ($select_aa = mysqli_fetch_assoc($select_query)); ?>
</select>

Related

how to get the data on script and load it in query

Why can't I get the value of the element? sorry I'm new at coding and trying to learn
<script>
function getdll() {
document.getElementById('lblmess').innerHTML =
(formid.listid[formid.listid.selectedIndex].value)
$element = document.getElementById("lblmess");
console.log($element.innerHTML)
}
</script>
<form name="formid">
<select class="custom-select" name="listid" onchange="getdll()">
<option value="0"></option>
<?php
$hall_qry = $conn->query("SELECT * FROM `office_hall` WHERE o_id !=0 order by `office_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['o_id'] ?>"><?php echo $row['office_name'] ?></option>
<?php
endwhile;
?>
</select>
<br><br>
<select class="custom-select">
<?php
$hall_qry = $conn->query("SELECT * FROM `assembly_hall` WHERE o_id = '$element' order by `room_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['room_name'] ?></option>
<?php
endwhile;
?>
</select>
<label id="lblmess"></label>
</form>
this is where I use this code sorry if my coding id is like that .............................................................
Try:
onchange="getdll(this.value)"
THEN in your script, put a parameter
function getdll(val){
alert(val);
}
Should be able to get the value you selected

How to set Selected value to dropdown list from database PHP/SQL

I want to set selected values in dropdown list to the one im editing.
$id = $_GET['edit'];
$result = $polaczenie->query("SELECT * FROM wizyty WHERE idwizyty=$id")
or die($mysqli->error());
while($row=mysqli_fetch_assoc($result))
{
$id = $row['idwizyty'];
$data = $row['data'];
$pracownik = $row['pracownik'];
$usluga = $row['usluga'];
$klient = $row['klient'];
$time = $row['starttime'];
$cena = $row['cena'];
}
Dropdown list
<label>Pracownik</label>
<?php
$query = "SELECT imie, nazwisko FROM `pracownicy`";
$wynik1 = mysqli_query($polaczenie, $query);
?>
<select name="nowypracownik" value="<?php echo $pracownik; ?>">
<?php while($row1 = mysqli_fetch_array($wynik1)):;?>
<option selected="<?php echo $pracownik?>"><?php echo $row1[0] .' '. $row1[1] ;?></option>
<?php endwhile; ?>
</select><br><br>
I tried to do it like this but it's not working.
The only value for ‘selected’ attribute is ‘selected’
<option value="value" selected="selected">value</option>
So your code should look something like this
<select name="nowypracownik" value="<?php echo $pracownik; ?>">
<?php while($row1 = mysqli_fetch_array($wynik1));?>
$value = $row1[0] .' '. $row1[1];
$selected = $value == $pracownik ? 'selected="selected"' : '';
<option <?php echo $selected?>><?php echo $value;?></option>
<?php endwhile; ?>
</select><br><br>

Select options taking same value every time

Looping from database duplicating the option values in options tag.
Please help me out.
My code is below.
<input type="hidden" name="beautician<?php echo $data[0]; ?>[]" id="beautician">
<select name="beautician[<?php echo $data[0]; ?>]" id="beauty" onchange="pvalue()">
<option value="null">Select</option>
<?php
$query = "select id,title from beautician order by id";
$run = mysql_query($query);
while ($row = mysql_fetch_array($run)) {
$id = $row[0];
$name = $row[1];
?>
<option value="<?php echo $id; ?>"> <?php echo $name; ?> </option>
<?php } ?>
</select>
whenever the user is selecting beautician it is taking id of the first option.
If id of the first selected beautician is 6 then every other beautician will have id 6
Because you forgot to print $id
Correct
<option value="<?php $id; ?>"> <?php echo $name; ?> </option>
To
<option value="<?php echo $id; ?>"> <?php echo $name; ?> </option>
Instead this:
<input type="hidden" name="beautician<?php echo $data[0]; ?>[]" id="beautician">
<select name="beautician[<?php echo $data[0]; ?>]" id="beauty" onchange="pvalue()">
<option value="null">Select</option>
<?php
$query = "select id,title from beautician order by id";
$run = mysql_query($query);
while ($row = mysql_fetch_array($run)) {
$id = $row[0];
$name = $row[1];
?>
<option value="<?php $id; ?>"> <?php echo $name; ?> </option>
<?php } ?>
</select>
Try this:
<input type="hidden" name="beautician <?php echo $data[0]; ?>[]" id="beautician">
<select name="beautician[<?php echo $data[0]; ?>]" id="beauty" onchange="pvalue()">
<option value="null">Select</option>
<?php
$query = "select id,title from beautician order by id";
$run = mysql_query($query);
while ($row = mysql_fetch_array($run)) {
$id = $row['id'];
$name = $row['title'];
?>
<option value="<?php echo $id; ?>"> <?php echo $name; ?> </option>
<?php } ?>
</select>
In the below statement you are not echoing $id in value. To get the value you have to <?php echo $id;?> OR <?=$id?>. Using your existing statement you have not printed the $id so the select option will take the <option> tag's text value, In your case value will be from $name variable.
Your statement:
<option value="<?php $id; ?>"> <?php echo $name; ?> </option>
Correct Statement:
<option value="<?php echo $id; ?>"> <?php echo $name; ?> </option>
As per my thinking you are getting value from the pvalue() on the change event of <select> tag and in this function you might be getting value based on the #beauty selector, because of this you are getting first dropdown value this happens because of duplicate id selector #beauty
Checkout below fiddle. There are two dropdowns and those have onchange="pvalue(this)" function on change event. Instead of no parameters in pvalue() function I have passed this in pvalue(this) function. Based on this parameter it will give you current <select> option.
<select name="beautician[0]" id="beauty" onchange="pvalue(this)">
<option value="null">Select</option>
<option value="one - 1">One - 1</option>
<option value="Two - 1">Two - 1</option>
</select>
<select name="beautician[1]" id="beauty" onchange="pvalue(this)">
<option value="null">Select</option>
<option value="one - 2">One - 2</option>
<option value="Two - 2">Two - 2</option>
</select>
<script>
function pvalue(obj, rowid){
var x = obj.value;
var y = document.getElementById('beautician'+ ).value = x;
alert(x);
}
</script>
<input type="hidden" name="beautician<?php echo $data[0]; ?>[]" id="beautician-<?php echo $data[0]; ?>">
<select name="beautician[<?php echo $data[0]; ?>]" id="beauty" onchange="pvalue(this, '<?php echo $data[0]; ?>')">
<option value="null">Select</option>
</select>
<script>
function pvalue(obj, rowid) {
var x = obj.value;
var y = document.getElementById('beautician-' + rowid).value = x;
}
</script>
This line:
<option value="<?php $id; ?>"> <?php echo $name; ?> </option>
Should be:
<option value="<?php echo $id; ?>"> <?php echo $name; ?> </option>

How to set selected option in dropdown

This will echo dropdown value take from database that
<select name="PACKAGE_ID" id="PACKAGE_ID" ng-model="FormData.Phases" class="form-control" required>
<?php
$result=mysqli_query($conn, "select * from unifi WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {
?>
<option name="UNIFI" value="<?php echo $row["PACKAGE_NAME"]; ?>">
<?php echo $row[ "PACKAGE_NAME"]; ?>
</option>
<?php } ?>
<?php $result=mysqli_query($conn, "select * from streamyx WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
<option name="STREAMYX" value="<?php echo $row["PACKAGE_NAME"]; ?>">
<?php echo $row[ "PACKAGE_NAME"]; ?>
</option>
<?php } ?>
<?php $result=mysqli_query($conn, "select * from webe WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
<option name="WEBE" value="<?php echo $row["PACKAGE_NAME"]; ?>">
<?php echo $row[ "PACKAGE_NAME"]; ?>
</option>
<?php } ?>
<?php $result=mysqli_query($conn, "select * from dome WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
<option name="WEBE" value="<?php echo $row["PACKAGE_NAME"]; ?>">
<?php echo $row[ "PACKAGE_NAME"]; ?>
</option>
<?php } ?>
below is how im taking value from database based on ticket id
$sql = "SELECT * FROM cusinfo WHERE TICKET_ID = '".$strid."' ";
$query = mysqli_query($conn,$sql);
$result=mysqli_fetch_array($query,MYSQLI_ASSOC);
I want to echo selected value for eg.<<< $result["PACKAGE_ID"]==$row[ "PACKAGE_NAME"] echo selected >>
Because you are using the $result variable many times with subsequent queries, you will need to preserve $result["PACKAGE_CATEGORY"] as a new variable, like this:
$selected=$result["PACKAGE_CATEGORY"];
You can write inline condition statements like this:
echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"",($row[ "PACKAGE_NAME"]==$selected?" selected":""),">{$row["PACKAGE_NAME"]}</option>";
This means if the condition is true then selected will be echoed, if false an empty string will be echoed.
The same technique on multiple lines would look like this:
echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"";
echo $row["PACKAGE_NAME"]==$selected?" selected":"";
echo ">{$row["PACKAGE_NAME"]}</option>";
If you don't want to use an inline condition here is the standard syntax:
echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"";
if($row["PACKAGE_NAME"]==$selected){
echo " selected";
}
echo ">{$row["PACKAGE_NAME"]}</option>";

How to show the value of the database to combobox?

How to show the value of the database to combobox? I've tried like this:
http://pastebin.com/gZL4qAPS
$result = $koneksi->query(
"SELECT tb_sekolah.idSekolah,
tb_sekolah.namaSekolah,
tb_sekolah.tb_kategori_sekolah_idKategori,
tb_kategori_sekolah.namaKategori,
tb_uptd.namaUPTD,
tb_sekolah.alamat,
tb_sekolah.telp,
tb_sekolah.kataSandi,
tb_sekolah.status
FROM tb_sekolah, tb_kategori_sekolah, tb_uptd
WHERE tb_sekolah.tb_kategori_sekolah_idKategori = tb_kategori_sekolah.idKategori
AND tb_uptd.idUPTD = tb_sekolah.tb_UPTD_idUPTD
AND idSekolah='$id'"
);
while ($row = $result->fetch_array()){
<select name="id_kategori" size="1" class="form-control" required>
<option label="-- Pilih Kategori --" ></option>
<?php //looping kategori
$result1 = $koneksi->query("SELECT * FROM tb_kategori_sekolah");
while ($row1 = $result1->fetch_array()){
if ($row['tb_kategori_sekolah_idKategori']==$row1['idKategori']){
$status = 'selected' ;
} ?>
<option <?php echo isset($status)?$status:''; ?> value="<?php echo $row1['idKategori'] ?>"><?php echo $row1['namaKategori']; ?>
</option>
<?php
}
?>
</select>
<?php
}
?>
were selected to always be the last value of tb_kategori_sekolah
it is simply because you have not set declare
$status = NULL;
inside the While function
your code suppose to be like this
<select name="id_kategori" size="1" class="form-control" required>
<option label="-- Pilih Kategori --" ></option>
<?php //looping kategori
$result1 = $koneksi->query("SELECT * FROM tb_kategori_sekolah");
while ($row1 = $result1->fetch_array()){
// DECLARE THIS AS NULL
$status = NULL;
if ($row['tb_kategori_sekolah_idKategori']==$row1['idKategori']){
$status = 'selected' ;
} ?>
<option <?php echo isset($status)?$status:''; ?> value="<?php echo $row1['idKategori'] ?>"><?php echo $row1['namaKategori']; ?>
</option>
<?php
}
?>
</select>

Categories