Select options taking same value every time - php

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>

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

avoiding the display of dropdown value twice in edit

While editing the record from database, i display already selected value and option to select other values too. But i want to avoid already selected value to display twice in dropdown. Not getting how to do it
here is my code
<label class="control-label">Sales Area</label>
<?php
$sql5 = "SELECT * FROM sales_area ORDER BY name";
$query5 = mysqli_query($con, $sql5);
?>
<select name="area" class="form-control" required>
<option value="<?php echo $row['sales_area']; ?>"><?php echo $row['areaname']; ?></option>
<?php while ($rs5 = mysqli_fetch_array($query5)) { ?>
<option value="<?php echo $rs5["id"]; ?>"><?php echo $rs5["name"]; ?></option>
<?php } ?>
</select>
in $row['sales_area'], data already present in database, this should not display again.
haven't tested it, but should be something like this:
<select name="area" class="form-control" required>
<?php while ($rs5 = mysqli_fetch_array($query5)) { ?>
<option value="<?php echo $rs5["id"]; ?>" <?php if($rs5["id"] == $row['sales_area'] ) { echo "selected"; } ?> ><?php echo $rs5["name"]; ?>
</option>
<?php } ?>
</select>

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>

How to Add scroll bar to html autocomplete datalist element?

Here is my PHP Code, I am trying to add a scroll bar to my autocomplete search, I have retrieved data from database and wanted to add a scroll bar.:
<?php
include('connection.php');
$result1 = mysql_query("select * from legal_pashto");
$result2 = mysql_query("select * from legal_dari");
$result3 = mysql_query("select * from pashto_pashto");
$result4 = mysql_query("select * from dari_pashto");
?>
<datalist id="words">
<?php while($row = mysql_fetch_assoc($result1)){?>
<option value="<?php print $row['english_word']; ?>
<option value="<?php print $row['pashto_word']; ?>
<option value="<?php print $row['pashto_word1']; ?>
<option value="<?php print $row['pashto_word2']; ?>
<option value="<?php print $row['pashto_word3']; ?>
<option value="<?php print $row['pashto_word4']; ?>
<?php }?>
<?php while($row2 = mysql_fetch_assoc($result2)){?>
<option value="<?php print $row2['english_word']; ?>
<option value="<?php print $row2['dari_word']; ?>
<option value="<?php print $row2['dari_word1']; ?>
<option value="<?php print $row2['dari_word2']; ?>
<option value="<?php print $row2['dari_word3']; ?>
<option value="<?php print $row2['dari_word4']; ?>
<?php }?>
<?php while($row3 = mysql_fetch_assoc($result3)){?>
<option value="<?php print $row3['pashto_word']; ?>
<option value="<?php print $row3['pashto_meaning']; ?>
<option value="<?php print $row3['pashto_meaning1']; ?>
<option value="<?php print $row3['pashto_meaning2']; ?>
<option value="<?php print $row3['pashto_meaning3']; ?>
<option value="<?php print $row3['pashto_meaning4']; ?>
<?php }?>
<?php while($row4 = mysql_fetch_assoc($result4)){?>
<option value="<?php print $row4['dariword']; ?>
<option value="<?php print $row4['darimeaning']; ?>
<option value="<?php print $row4['darimeaning1']; ?>
<option value="<?php print $row4['darimeaning2']; ?>
<option value="<?php print $row4['darimeaning3']; ?>
<option value="<?php print $row4['darimeaning4']; ?>
<?php }?>
</datalist>
I am not very good when it comes to html but in my opinion adding your PHP code may help other help you. Just saying.

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