How to show the value of the database to combobox? - php

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>

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 display specific fields from db using the selected value in dropdown listbox in php

<select id="qualification" class="form-control">
<option selected="selected">SELECT</option>
<option value="pick"</option>
<?php $sql = mysqli_query($con, "SELECT DISTINCT qualification From
enquire");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql))
{ echo "<option value='". $row['qualification'] ."'>".$row['qualification']."</option>" ; } ?>
</select>
how to retrieve fields in table format from database using qualification ?
<?php
$sql="SELECT Option1, option2 FROM yourTable";
$result = mysqli_query($YourDBconnection,$sql);
?>
your SELECT like
<select>
<?php
while ($row = mysql_fetch_assoc($result)){
echo'<option>'.$row['option1'].'<option>';
}
?>
</select>
Do this
<select id="qualification" class="form-control">
<option selected="selected">SELECT</option>
<option value="pick"</option>
<?php $sql = mysqli_query($con, "SELECT DISTINCT qualification From
enquire");
//$row = mysqli_num_rows($sql);
while ($row = mysql_fetch_assoc($sql)))
{ ?>
<option value="<?php echo $row['qualification']; ?>"> <?php echo $row['qualification']; ?> </option>
<?php } ?>
</select>
You may wanna reconsider your codes and use prepared statement to avoid SQL injection.
The below code is prepared statement
<select id="qualification" class="form-control">
<option selected="selected">SELECT</option>
<option value="pick"</option>
<?php $sql =$con->prepare("SELECT DISTINCT qualification From
enquire");
$sql->execute();
$result = $sql->get_result();
while ($row = $result->fetch_assoc())
{ ?>
<option value="<?php echo $row['qualification']; ?>"> <?php echo $row['qualification']; ?> </option>
<?php } ?>
</select>

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>

Set first value of dynamic HTML option to empty

<select id="section" name="section">
<?php
include("Nethost.php");
$section = "";
$yr = "";
$sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
while ($row = mysql_fetch_array($sql)){
$section = $row['section'];
$yr = $row['yrlvl'];
?>
<option value="">Select</option>
<option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
if(mysql_num_rows($result2) > 0) { ?>
selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
<?php } ?>
</select>
Above is the php code of the select option. Populated with data from database table, how can I set that the first value if empty. I tried adding a Select but this is the result.
The option select keeps repeating. What to do with this?
Try this
<select id="section" name="section">
<?php
include("Nethost.php");
$section = "";
$yr = "";
$sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
?>
<option value="">Select</option>
<?php
while ($row = mysql_fetch_array($sql)){
$section = $row['section'];
$yr = $row['yrlvl'];
?>
<option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
if(mysql_num_rows($result2) > 0) { ?>
selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
<?php } ?>
</select>
Just move that option line "Select" out of your php code:
<select id="section" name="section">
<option value="">Select</option>
<?php
...your php code
?>
</select>

selected value get from db into dropdown select box option using php mysql error

I need to get selected value from db into select box. please, tell me how to do it. Here is the code.
Note: 'options' value depends on the category.
<?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res)){
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
</select>
<?php
}
?>
I think you are looking for below code changes:
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php if($options=="PHP") echo 'selected="selected"'; ?> >PHP</option>
<option value="ASP" <?php if($options=="ASP") echo 'selected="selected"'; ?> >ASP</option>
</select>
The easiest way I can think of is the following:
<?php
$selection = array('PHP', 'ASP');
echo '<select>
<option value="0">Please Select Option</option>';
foreach ($selection as $selection) {
$selected = ($options == $selection) ? "selected" : "";
echo '<option '.$selected.' value="'.$selection.'">'.$selection.'</option>';
}
echo '</select>';
The code basically places all of your options in an array which are called upon in the foreach loop. The loop checks to see if your $options variable matches the current selection it's on, if it's a match then $selected will = selected, if not then it is set as blank. Finally the option tag is returned containing the selection from the array and if that particular selection is equal to your $options variable, it's set as the selected option.
for example ..and please use mysqli() next time because mysql() is deprecated.
<?php
$select="select * from tbl_assign where id='".$_GET['uid']."'";
$q=mysql_query($select) or die($select);
$row=mysql_fetch_array($q);
?>
<select name="sclient" id="sclient" class="reginput"/>
<option value="">Select Client</option>
<?php $s="select * from tbl_new_user where type='client'";
$q=mysql_query($s) or die($s);
while($rw=mysql_fetch_array($q))
{ ?>
<option value="<?php echo $rw['login_name']; ?>"<?php if($row['clientname']==$rw['login_name']) echo 'selected="selected"'; ?>><?php echo $rw['login_name']; ?></option>
<?php } ?>
</select>
Just Add an extra hidden option and print selected value from database
<option value="<?php echo $options;?>" hidden><?php echo $options;?></option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
Select value from drop down.
<select class="form-control" name="category" id="sel1">
<?php foreach($data as $key =>$value) { ?>
<option value="<?php echo $data[$key]->name; ?>"<?php if($id_name[0]->p_name==$data[$key]->name) echo 'selected="selected"'; ?>><?php echo $data[$key]->name; ?></option>
<?php } ?>
</select>
THE EASIEST SOLUTION
It will add an extra in your options but your problem will be solved.
<?php
if ($editing == Yes) {
echo "<option value=\".$MyValue.\" SELECTED>".$MyValue."</option>";
}
?>
$option = $result['semester'];
<option >Select</option>
<option value="1st" <?php if($option == "1st") echo 'selected = "selected"'; ?>>1st</option>
<option value="2nd" <?php if($option == "2nd") echo 'selected = "selected"'; ?>>2nd</option>
<option value="3rd" <?php if($option == "3rd") echo 'selected = "selected"'; ?>>3rd</option>
<option value="4th" <?php if($option == "4th") echo 'selected = "selected"'; ?>>4th</option>
<option value="5th" <?php if($option == "5th") echo 'selected = "selected"'; ?>>5th</option>
<option value="6th" <?php if($option == "6th") echo 'selected = "selected"'; ?>>6th</option>
<option value="7th" <?php if($option == "7th") echo 'selected = "selected"'; ?>>7th</option>
<option value="8th" <?php if($option == "8th") echo 'selected = "selected"'; ?>>8th</option>
</select>
BEST code and simple
<select id="example-getting-started" multiple="multiple" name="category">
<?php
$query = "select * from mine";
$results = mysql_query($query);
while ($rows = mysql_fetch_assoc(#$results)){
?>
<option value="<?php echo $rows['category'];?>"><?php echo $rows['category'];?></option>
<?php
}
?>
</select>
You can also do like this ....
<?php $countryname = $all_meta_for_user['country']; ?>
<select id="mycountry" name="country" class="user">
<?php $myrows = $wpdb->get_results( "SELECT * FROM wp_countries order by country_name" );
foreach($myrows as $rows){
if( $countryname == $rows->id ){
echo "<option selected = 'selected' value='".$rows->id."'>".$rows->country_name."</option>";
} else{
echo "<option value='".$rows->id."'>".$rows->country_name."</option>";
}
}
?>
</select>
Answer is simple.
when u pass value from dropdown.
Just use as if else.
for eg:
foreach($result as $row) {
$GLOBALS['output'] .='<option value="'.$row["dropdownid"].'"'.
($GLOBALS['passselectedvalueid']==$row["dropwdownid"] ? ' Selected' : '').'
>'.$row['valueetc'].'</option>';
}
<?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res)){
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php echo $options == 'PHP' ? 'selected' : ''; ?> >PHP</option>
<option value="ASP" <?php echo $options == 'ASP' ? 'selected' : ''; ?> >ASP</option>
</select>
<?php
}
?>
USING PDO
<?php
$username = "root";
$password = "";
$db = "db_name";
$dns = "mysql:host=localhost;dbname=$db;charset=utf8mb4";
$conn = new PDO($dns,$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "select * from mine where username = ? ";
$stmt1 = $conn->prepare($sql);
$stmt1->execute(array($_POST['user']));
$all = $stmt1->fetchAll(); ?>
<div class="controls">
<select data-rel="chosen" name="degree_id" id="selectError">
<?php
foreach($all as $nt) {
echo "<option value =$nt[id]>$nt[name]</option>";
}
?>
</select>
</div>
I'm using eval() PHP function like this:
My PHP code:
$selOps1 = $selOps2 = $selOps3 = '';
eval('$selOps'. $dbRow["DBitem"] . ' = "selected";');
Then in my select box I use it like this:
<select>
<option <?=$selOps1?> value="1">big</option>
<option <?=$selOps2?> value="2">Middle</option>
<option <?=$selOps3?> value="3">Small</option>
</select>
Put value from db into a variable and check like following code example
<select class="form-control" name="currency_selling" required >
<option value="">Select Currency</option>
<option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
<option value="dollar" <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
<option value="pounds" <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
<option value="dirham" <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
</select>
This may help you.
?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res))
{
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
// Assuming $list['options'] is a coma seperated options string
$arr=explode(",",$list['options']);
<?php foreach ($arr as $value) { ?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } >
</select>
<?php
}
?>

Categories