I have a form with a multiple select inside a bootstrap modal:
<label>Product:</label>
<select name="product[]" multiple="multiple" id="product">
<option value="1">Product 1</option>
<option value="2">Product 2</option>
<option value="3">Product 3</option>
<option value="4">Product 4</option>
<option value="5">Product 5</option>
<option value="6">Product 6</option>
</select><br />
I'll save my data from this form to a single field in a Mysql database like 1, 4, 6 (I used implode to do this)
Now when I call a modal to edit my form, I want to see the multiple select with the values 1, 4 and 6 selected.
Is there a way to achieve this?
EDIT:
Thanks for the quick answers! All were useful, but when I'm using a lot of modals, maybe it's better to call them using javascript and fill the fields in the form like:
HTML:
<a href='#afvalstoffen-edit' class='afvalstoffen-edit' data-toggle='modal' data-id='$id' data-product='$product'......
JAVASCRIPT:
$(document).on("click", ".afvalstoffen-edit", function () {
var data_id= $(this).data('id');
var product= $(this).data('product');.....
And then fill the form in the modal like:
......
$(".modal-body #data_id").val( data_id);
$(".modal-body #product").val( product);
$('#afvalstoffen-edit').modal('show');
});
Is there also a way to get the values of the multiple select selected this way?
SOLUTION:
var product = "product1, product2, product3";
var product_array = product.split(", ");
$(".modal-body #product").val( product_array );
DEMO:
I have a form with a multiple select inside a bootstrap modal:
<label>Product:</label>
<select name="product[]" multiple="multiple" id="product">
<option value="1">Product 1</option>
<option value="2">Product 2</option>
<option value="3">Product 3</option>
<option value="4">Product 4</option>
<option value="5">Product 5</option>
<option value="6">Product 6</option>
</select><br />
I'll save my data from this form to a single field in a Mysql database like 1, 4, 6 (I used implode to do this)
Now when I call a modal to edit my form, I want to see the multiple select with the values 1, 4 and 6 selected.
Is there a way to achieve this?
EDIT:
Thanks for the quick answers! All were useful, but when I'm using a lot of modals, maybe it's better to call them using javascript and fill the fields in the form like:
HTML:
<a href='#afvalstoffen-edit' class='afvalstoffen-edit' data-toggle='modal' data-id='$id' data-product='$product'......
JAVASCRIPT:
$(document).on("click", ".afvalstoffen-edit", function () {
var data_id= $(this).data('id');
var product= $(this).data('product');.....
And then fill the form in the modal like:
......
$(".modal-body #data_id").val( data_id);
$(".modal-body #product").val( product);
$('#afvalstoffen-edit').modal('show');
});
Is there also a way to get the values of the multiple select selected this way?
SOLUTION:
var product = "product1, product2, product3";
var product_array = product.split(", ");
$(".modal-body #product").val( product_array );
DEMO:
http://jsfiddle/8WhrA
Share Improve this question edited May 29, 2013 at 11:18 JK87 asked May 28, 2013 at 13:46 JK87JK87 3792 gold badges12 silver badges28 bronze badges 1- 1 Explode data from Database and check each option, to example: <option value="<?php echo $value = 1; ?>" <?php if (in_array($value, $explodedData)): ?>selected="selected"<?php endif; ?>>Product 1</option> – maximkou Commented May 28, 2013 at 13:51
2 Answers
Reset to default 3Use a loop:
// Grab the selected values from the database, if its a string, use 'explode()' to make them an array
$selectedValues = array(1, 3, 5);
$selectOptions = array(
'1' => 'Product 1',
'2' => 'Product 2',
'3' => 'Product 3',
'4' => 'Product 4',
'5' => 'Product 5'
);
$html = '<select>';
foreach($selectOptions as $key => $value)
{
$selected = in_array($key, $selectedValues) ? 'selected ' : '';
$html .= '<option ' . $selected . 'value="' . $key . '">' . $value . '</option>';
}
$html = '</select>';
echo $html;
You'll have to add selected inside the option that has to be selected, this looks like:
<label>Product:</label>
<select name="product[]" multiple="multiple" id="product">
<option value="1" selected>Product 1</option>
<option value="2">Product 2</option>
<option value="3">Product 3</option>
<option value="4" selected>Product 4</option>
<option value="5">Product 5</option>
<option value="6" selected >Productv6</option>
</select><br />
So write some php code adding selected to corresponding tag if it has to be selected by default.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745225788a4617455.html
评论列表(0条)