I have a multiple select dropdown with checkbox. But I am not sure how to insert it into database and the next time the page opens, it displays the current value from the database.
How can I insert values from my multiple select dropdown into database and display it as checked?
<div class="form-group">
<div align="left">
<div class="formlist">Purchase Group :
<select id="purchase_group" name="purchase_group" class="form-control" multiple="multiple">
<?php
$query_pgr = "SELECT * FROM purchasing_group WHERE pgr_enable=1 ORDER BY pgr_name";
$rs_pgr = DB_Query($query_pgr);
while ($row_pgr = DB_FetchRow($rs_pgr)) {
$pgr.='<option value='.$row_pgr["pgr_id"].'>' .$row_pgr["pgr_name"].' </option>';
}
mysql_free_result($rs_pgr);
echo $pgr;
?>
</select>
</div>
</div>
</div>
this is the display of my multiple dropdown.
$('#btnSelected').click(function () {
var selected = $("#purchase_group option:selected");
var message = "";
selected.each(function () {
message += $(this).text() + " " + $(this).val() + "\n";
});
alert(message);
});
Every multiple dropdown plug-in hides the <select> with their own dropdown, what ever selection you do can be fetched in php like:
$selection = $_REQUEST['purchase_group'];
here $selection is an array you can use foreach() to get its values and insert it into database.
Note: To hold multiple values, the name of the <select> must be an array like:
<select id="purchase_group" name="purchase_group[]" class="form-control" multiple="multiple">
Related
When I choose an option in my dropdown, I want to take it's MySQL table ID and put it in a hidden input box. The MySQL table rows are correct, but the input box never changes.
I'm using this to try to retrieve the value:
$('.theNum').click(function() {
$('#menu2').html($(this).text() + '<span class="caret"></span>')
})
$(function() {
//Listen for a click on any of the dropdown items
$(".theNum").click(function() {
//Get the value
var value = $(this).attr("value");
//Put the retrieved value into the hidden input
$("input[name='theNum']").val(value);
});
});
And this HTML code, with the hidden input box:
<div class="form-group" >
<select name="companies" class="form-control " id="companies" >
<option value="" disabled selected id="menu2"> Selecione a empresa </option>
<?php
while ($row = $result->fetch_assoc()) : ?>
<option name = 'theNum' value="<?= $row['id']; ?>"> <?= $row['nome']; ?> </option>
<?php endwhile ?>
</select>
</div>
Thank you for your help!
You don't listen for clicks on options. You should listen for change of the <select>, and get its value.
$("#companies").change(function() {
$("input[name='theNum']").val($(this).val());
});
I have problem when I get value from left join table, database from product table (https://ibb.co/zbpJP0z )and database from farm table !(https://ibb.co/gMKySpP )
I've tried to get the value from dropdown list, but the value is always from joining table
this is my form code
<form class="mb-2" id="addproductform" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="serial-number">Serial Number:</label>
<select class="serial-number form-control" name="serial-number" class="custom-select mb-3" id="newval">
<option>Serial Number</option>
<?php $q=mysqli_query($link,"SELECT produk.nama FROM produk LEFT JOIN farm ON produk.SN=farm.SN WHERE username=\"$_SESSION[username]\"");
while($d=mysqli_fetch_row($q)) {
echo "<option value=$d[0]>$d[0]</option>";
} ?>
</select>
</div>
</form>
..........................
and this is a jquery code
$(document).ready(function(e){
$("#save_addPlant").click(function(e){
e.preventDefault();
console.log("submit productform");
$("select.serial-number").change(function(){
var tp = $(this).children("option:selected").val();
});
namafarm=$("option:selected").val();
console.log(namafarm);
});
});
How can I take a value from farm.SN, while I display produk.nama in the dropdown list?
There are 2 places that need change in your code.
Your are using 2 class attributes in your select tag. The second
class attribute wont work. So you have to combine the 2 class
attributes into 1.
You are only selecting produk.nama column in your select query. You
must also select produk.SN column.
Replace your select block with the code below & check whether it works.
<select class="custom-select mb-3 form-control serial-number" name="serial-number" class="" id="newval">
<option>Serial Number</option>
<?php
$q = mysqli_query( $link, "SELECT produk.SN,produk.nama FROM produk INNER JOIN farm ON produk.SN = farm.SN WHERE username = \"$_SESSION[username]\"" );
while($d=mysqli_fetch_row($q)) {
echo "<option value=$d[0]>$d[1]</option>";
}
?>
I have a form in which when the user chooses certain option in a select drop-down, a new field should shows up for entering another piece of information.
Basically, it's the common "Where did you hear about us?" and when the user selects other, a new empty field is shown so that he can type the source if it wasn't in the list.
But I really have no clue about how to do it. I've been using PHP and HTML to do the form and validating it.
To add some code, this is where I create the select
<label for="where_heard">Where heard:</label>
<select name="where_heard"> <?php echo $sources; ?>
</select>
And this is where I get the source list from:
$query = "SELECT source FROM where_heard";
$stmt = $db->prepare($query);
$stmt->execute();
$sources = "";
while ($row = $stmt->fetch()) {
$sources .= '<option value= "'.$row['source'].'">'.$row['source'].'</option>';
}
You can do this with jquery like:
$( document ).ready(function() {
$('select').on("change",function(){
if($(this).val() == "other")
$(".other").html("Other: <input type='text' name='other'/>");
else
$(".other").html("");
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="where_heard">Where heard:</label>
<select name="where_heard"><option></option><option value="other">Other</option>
</select>
<div class="other"></div>
I want view a price values in a field when select a record from dropdown list.
This is the dropdown list code with php code inside:
<select name="product_id[]" class="form-control">
<?php
include("connect.php");
$query = "
SELECT *
FROM tbl_product ORDER BY product_desc ASC
";
$result = mysql_query($query);
while ($record = mysql_fetch_array($result)) {
echo "
<option value=\"$record[product_id]\">$record[product_cod] $record[product_desc]</option>";
}
?>
</select> <input type="text" name="product_price[]" size="6">
In to the specific case: in dropdown list I have 3 records products:
1 milk 3euro
2 wather 1euro
3 caffee 4euro
For example, when select the second record, in the filed price how to put the 4euros values?
Thanks
Well you could try using something like this. My code uses ajax to get the price of the product_id we got from select.
<?php
include("connect.php");
$query = "SELECT * FROM tbl_product ORDER BY product_desc ASC";
$result = mysql_query($query);
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<select name="product_id[]" class="form-control">
<?php
while ($record = mysql_fetch_array($result))
{
?>
<option value="<?php echo $record['product_id']; ?>"><?php echo $record['product_cod'] . " " . $record['product_desc']; ?></option>
<?php
}
?>
</select> <input type="text" id="input_price" name="product_price[]" size="6" value="">
<script>
$( "select" )
.change(function () {
var val = $( "select option:selected").val();
//$('#input_price').val(val);
$.ajax({
url : 'ajax_getprice.php',
type : 'GET',
data : {
'product_id' : val
},
dataType:'json',
success : function(data) {
$('#input_price').val(data);
},
error : function(request,error)
{
alert("Request: "+JSON.stringify(request));
}
});
})
.change();
</script>
ajax_priceget.php
<?php
$productid = $_GET['product_id']; //you have the product id now
//now write a query to select the price of the product using the product id and echo the price
?>
#Frankie, if the price field is separetly shown somewhere then there are two ways by which you can perform this,
1- Use jquery to get the prices, in order to do it through jquery you need to place the prices of the records in their values and then to use $('.product_id[]').val(); to get the price of the selected record.
2- Second way is to use the ajax for that purpose if you have stored item's prices in the databse then you need to hit the database by sitting at the front end at the onChange's jquery method you need to get ajax response regarding the prices against the product id,s and then to display at any posisiton you want to.
I want to know that when I select any value from dropdown box it shows it's price in next field.
Example: if I select snacks from the dropdown menu then it show its price in next field by fetching its price from database in PHP. Items are also fetched from database.
Here is how I want when select a particular value:
Here is the code for fetching data from the database:
<?php
$eb = "SELECT EmployeeID,FirstName,LastName FROM employees";
$res = mysql_query($eb);
$no_records = mysql_num_rows($res);
if($no_records > 0)
{?>
<select name="EnteredBy" id="EnteredBy"><?php
while($row = mysql_fetch_array($res))
{
echo '<option value='.$row['EmployeeID'].'>'.$row['LastName'].",".$row['FirstName'].'</option>';
}
?>
</select>
<?php
}
?>
Something like the following should help you out. All you need to do is write an onchange function to update your "next field" with the value of the option selected.
<html>
<head>
<script type="text/javascript">
function changeValue() {
var price = document.getElementById('items').options[document.getElementById('items').selectedIndex].value;
document.getElementById('price').innerHTML= '$'+price;
}
</script>
</head>
<body onload="changeValue();">
Select Item:
<select id="items" onchange="changeValue();">
<option value="5">Snack</option>
<option value="1">Soda</option>
<option value="10">Meal</option>
</select>
<br/><br/>
Price:
<div id="price">
</div>
</body>
</html>