1

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

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> Commented May 28, 2013 at 13:51

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Seems like Phil Cross gave an example of a piece of code that does this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.