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

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

Related

selected value get from db into Data list option using php mysql

I need to get selected value from db into datalist box.Tell me how to do it. Here is the code.
<input list="Rank_Name" class="form-control" required>
<datalist id="Rank_Name">
<?php
$sel_cus = "select Rank_Name from ranks where Rank_Status=1";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {
?>
<option value="<?php echo $row['Rank_Name'];?>"></option>
<?php
}
?>
</datalist>
If i understood right , you need to select value in dropdownlist with other value also. You can achieve this by doing this
<?php
$select1="select Rank_Name from ranks where Rank_Status=1";
$q=mysqli_query($select1) or die($select1);
$row=mysqli_fetch_array($q); //here you are getting name of person whose rank is 1
?>
<datalist id="Rank_Name">
<?php
$s="select * from ranks ";
$q=mysqli_query($s) or die($s);
while($r=mysqli_fetch_array($q))
{ ?>
<option value="<?php echo $r['Rank_Name']; ?>"<?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?>>
<?php echo $r['Rank_Name']; ?>
</option>
<?php } ?>
</datalist>
In above code, this line <?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?> check if value are same from first query ,and if same then that option will be get selected automatically
<input list="Rank_Name" class="form-control" required>
<datalist id="Rank_Name">
<?php
$sel_cus = "select Rank_Name from ranks where Rank_Status=1";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {
echo "<option value=".$row['Rank_Name']."></option>";
}
?>
</datalist>
try this code.
im using echo <option> with while loop

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>

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 create query mysql in jquery like a clone

i have a query in php :
<div class="col-md-7" id="col_bahan">
<select name="bahan[]" id="bahan" class="form-control bahan_asli">
<option value="0">--pilih--</option>
<?php
$qry = $db->query("SELECT * FROM bahan");
while ($bahan = $qry->fetch_array()) {
$qry2 = $db->query("SELECT * FROM warna WHERE id_warna='$bahan[id_warna]'");
$warna = $qry2->fetch_array();
?>
<option value="<?php echo $bahan['id_bahan']; ?>"><?php echo $bahan['id_bahan']; ?> - <?php echo $bahan['nm_bahan'] . ' Warna ' . $warna['nm_warna']; ?></option>
<?php
}
?>
</select>
</div>
I want to copy/clone this code in jquery, then I render element in HTML. I don't like using .clone() for it,
Try this,
$("#div").html($("#col_bahan").html());

Javascript - How to change a new value on drop down

When we change the name="quantity" and the $product['price'] value will changing too. Here will have dynamic quantity and price. How to do that using jquery/javascript.
<?php
$select = "SELECT * FROM `products` ORDER BY `id` DESC LIMIT 10";
$query = mysql_query($select) or die(mysql_error());
?>
<ul>
<?php while($product = mysql_fetch_array($query)) { ?>
<li>
<p><?php echo $product['name'];?></p>
<p> Quantity
<select name="quantity">
<?php for($i=1;$i<=$product['quantity'];$i++) { ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
</p>
<p><?php echo $product['price'];?></p>
</li>
<?php } ?>
</ul>
Let me know :)
Using jQuery:
$(document).ready(function() {
$("#quantity_select").bind("change", function() {
selected_option = $("option:selected").val();
$("#counter").html(selected_option);
});
});
...
<select id="quantity_select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="counter"></div>
You can use text() instead of val() if you want to see actual text instead of value attribute .

Categories