How to Select box value selected from db values in boostrap multiselect js? - bootstrap-multiselect

How to selected Db values selected in select dropdown list like PHP in Multiselect boostrap dropdown?
For example Php coding,
$array = array("apple", "orange", "lemon");
<select>
<option value=""></option>
<?php
foreach ($array as $data) {
?>
<option value="<?php echo $data ?>" <?php if ($data == "orange") echo "selected"; ?>><?php echo $data ?></option>
<?php } ?>
In php db value select using Selected,How in Boostrap Multiselect js?
<button type="button" class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" title="None selected">
None selected
<b class="caret">
</b>
<ul class="multiselect-container dropdown-menu">
<li class="multiselect-item multiselect-all">
<a href="javascript:void(0);" class="multiselect-all">
<label class="checkbox">
<input type="checkbox" name="multiselect" value="multiselect-all">
Select all
</label>
</a>
</li>
<li>
<a href="javascript:void(0);">
<label class="checkbox">
<input type="checkbox" name="multiselect" value="cheese">
Cheese
</label>
</a>
</li>
How to set value in above boostrap multiselect js? like selected option in php?for example:if db value coming cheese means selected value in Multiselect Dropdown?

You can do that by using the select function of the plugin
$(document).ready(function() {
$('#example-getting-started').multiselect();
$('#example-getting-started').multiselect('select', ['cheese', 'pepperoni']);
});
jsbin

From my understandingof your problem: You are using PHP to generate the select with its option and you retrieve the option and their selected/unselected state form a database. Your code snippet should be fine, Bootstrap Multiselect automatically preserves selected options (both in multiple and single mode). Note that in single mode (i.e. single selection through radio buttons) the last selected option is preselected in Bootstrap Multiselect, while in multiple mode all selected options will be preselected.
<?php $array = array("apple", "orange", "lemon"); ?>
<select> <!-- Note that the multiple attribute needs to be present to allow multiple selection. -->
<!-- Removed the empty option, not sure what it was for ... -->
<?php foreach ($array as $data): ?>
<option value="<?php echo $data ?>" <?php if ($data == "orange") echo "selected=\"selected\""; ?>><?php echo $data ?></option>
<?php endforeach; ?>
<!-- Do not forget closing tag. -->
</select>
<script type="javascript/text">
$(document).ready(function() {
// Will preselect "orange" in single selection mode ...
$('select').multiselect({
includeSelectAllOption: true
});
});
</script>
After page load, the multiselect can be manipulated as described by lcycool.

Related

Is there a way I can auto fill an input form based on a selectbox drop down?

I'm trying to auto fill an input form based on the selection of a selectbox.
the auto fill text comes from my database
This is my code:
<div class="form-group">
<label for="kode_part" class="control-label">Kode Part <span><small class="text-danger">*This field is required</small></span></label>
<select class="form-control kode_part" name="kode_part" id="kode_part" required>
<?php if(count($tabel_part)){ ?>
<option></option>
<?php foreach ($tabel_part as $list){ ?>
<option data-info="<?php echo $list['kode_part'] ?>">
<?php echo $list['kode_part']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
</div>
text auto fill based on selection:
<div class="form-group">
<label for="nama_part" class="control-label">Nama Part*</label>
<input class="form-control form-control-sm" id="nama_part" name="nama_part" />
</div>
script:
<script>
$(document).ready(function() {
$('#kode_part').change(function(){
$('#nama_part').val( $(this).find('list:selected').data('nama_part') );
});
});
</script>
i've try that but nothing happens in my view.
Any help you can give I appreciate it
Try this,
$('#kode_part').on('change', function() {
//you can use this too
$('#nama_part').val( $('#kode_part').find(":selected").text());
});

How to set data-list option value in second text box using html & JS (onChange() )

I have 1 datalist containing food items Name & 2nd textbox to display price of selected food item . I'm not understanding how to make a function on datalist using js to onChange display price in textbox txtprice .
#I'm using codeigniter
<input list="browsers" id="browser" name="browser">
<datalist id="browsers" onchange="getComboA(this)">
<?php foreach($h as $row){ ?>
<option value="<?php echo $row->price;?>" >
<?php echo $row->menu_name;?>
</option>
<?php } ?>
</datalist>
<div class="col-12 col-md-2"><input type="text" placeholder="Price" class="form-control" id="txtPrice" name="txtPrice" value="<?php echo $row->price;?>" readonly >
</div>
eg : Item Name - Panner Tikka | Price - 150
It's quite easy actually, you could set it up like this :
<input list="browsers" id="browser" name="browser" onchange="setTxtPrice(this)">
<datalist id="browsers">
<?php foreach($h as $row){ ?>
<option value="<?php echo $row->price;?>" >
<?php echo $row->menu_name;?>
</option>
<?php } ?>
</datalist>
<div class="col-12 col-md-2">
<input type="text" placeholder="Price" class="form-control" id="txtPrice" name="txtPrice" value="<?php echo $row->price;?>" readonly >
</div>
<script type="text/javascript">
function setTxtPrice(input1) {
var input2 = document.getElementById('txtPrice');
input2.value = input1.value;
}
</script>
It is very easy to use onchange() in JS
Use this.value to get the value of datalist when option will change
<datalist id="browsers" onchange="PutValue(this.value)">
<?php foreach($h as $row){ ?>
<option value="<?php echo $row->price;?>" >
<?php echo $row->menu_name;?>
</option>
<?php } ?>
JS
function PutValue(Value){
$('#input_field_id').val(Value);
}

Magento - Change quantity label text to hours and have a dropdown

Need help on how to change the label "quantity" to "hours" and have a drop down. I have tier pricing on that particular product. I need to replace the text "quantity" to hours on that particular product alone. Help required. Thanks in advance.
To change label navigate to Backend > catalog > attribute > manage attribute Search for your targeted attribute. And edit on the left side on attribute information section > Manage Label/options and there you will find titles section and change the label.
Regarding changing attribute type to dropdown from Catalog Input Type for Store Owner once you assign and save an attribute you cannot change there after.
Edit
To change for only one product check for it and if is true print other label.
$_product->getAttributeText('quantity')
You can do it programatically in this way:
Add a custom phtml file for addtocart part under: app/design/frontend/[your_template_package_name]/[your_template_name]/template/catalog/product/view/addtocart_custom.phtml
with this content:
<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>
<?php if($_product->isSaleable()): ?>
<div class="add-to-cart">
<?php if(!$_product->isGrouped()): ?>
<label for="qty"><?php echo $this->__('Hours:') ?></label>
<select class="input-text qty" name="qty" id="qty">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<?php endif; ?>
<button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
<?php echo $this->getChildHtml('', true, true) ?>
</div>
<?php endif; ?>
Add on your product page in Magento admin the following layout update under Product details->Design->Custom layout update:
<reference name="product.info.addtocart">
<action method="setTemplate"><template>catalog/product/view/addtocart_custom.phtml</template></action>
</reference>
in my case (magento ce 1.9.2) the path to addtocart.phtml is:
app/design/frontend/YOURTEMPLATE/default/template/catalog/product/view
replace the word "Quantity" with your word(s) in a line (in my case - line# 32) that starts with:
<label for="qty">
gl,
H

How to get id and value for a dropdown menu from another table in php

I have two tables. "cat" & "image".
Database name is "phpimage".
'cat' table has two columns. "id" and "name".
I want to get 'id' and 'name' from 'cat' table for a drop down list
<select>
<option value="THIS SHOULD BE id">THIS SHOULD BE name</option>
</select>
And the image table has three column. "imgid" , "catid" and "title".
And I want to store that selected 'id' from the 'cat' table to 'catid' in the 'image' table.
PLEASE HELP ME....
PHP:
<?php
include "conn.php";
mysql_select_db($database_phpimage,$phpimage);
$qur="SELECT * FROM cat";
$res=mysql_query($qur,$phpimage);
$cat_name = $_GET['name'];
$cat_id = $_GET['id'];
?>
<html>
<body>
<?php while($row=mysql_fetch_array($res)) { ?>
<select name="image_upload">
<option value="<?php echo $cat_id; ?>">
<?php echo $cat_name; ?>
</option>
</select>
<?php } ?>
</body>
</html>
I Found the solution. Anyway Thanks Stack Overflow.
Here is the code I found.
<?php
include "conn.php";
error_reporting(0);
?>
<html>
<body>
<?php
mysql_select_db($database_phpimage,$phpimage);
$qur="SELECT id,name FROM cat";
$res=mysql_query($qur,$phpimage);
?>
<select name="image_upload">
<option id="0">-- Select an Option --</option>
<?php
$getData = mysql_query("SELECT * FROM cat");
while($viewData = mysql_fetch_array($getData))
{ ?>
<option id="<?php echo $viewData['id']; ?>"><?php echo $viewData['name']; ?></option>
<?php } ?>
?>
</select>
</body>
</html>
That's the code I needed. Thanks.

Ci not outputting every mysql row into a selectbox

In Codeigniter how would I output all my database values into a selectbox? When I do the following I only get the first row outputted. It seems to cut off after the first. Any help?
<body>
<form method="post" action="<?=base_url()?>index.php/admin/addImg">
img url <input name="imgURL" type="text" /><br />
<br /> select project <select name="selectedProject">
<?php $query = $this->db->query("SELECT * FROM projects");
foreach ($query->result() as $row) { ?>
<option><?php echo $row->projectName.$row->projectId ?></option>
</select></form>
<?php } ?>
</body>
You're closing your select within the first loop. Try something like this:
<select name="selectedProject">
<?php foreach($query->result() as $row) {
<option><?php echo $row->projectName.$row->projectId ?></option>
<?php } ?>
</select>
Also, CodeIgniter provides a handy Form Helper for this type of thing.

Resources