简体   繁体   中英

Multiple select form values selected based on mysql values

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.net/8WhrA

Use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM