lou / multi-select

A user-friendlier drop-in replacement for the standard select with multiple attribute activated.
loudev.com
MIT License
1.9k stars 439 forks source link

Retrieve selected values from database #160

Open jsolivellase opened 9 years ago

jsolivellase commented 9 years ago

I have this code:

<script type="text/javascript">
        jQuery(function ($) {
            $('#sensor_id').multiSelect();

            $(document).ready(function() {
                $("#basin_id").trigger("change");

                var $selected = <?php echo json_encode($station); ?>;
                $("#station_id").each(function() {
                    if ($(this).text() == $selected.id)
                        $(this).attr("selected","selected");
                });

                var $selected = <?php echo json_encode($datalogger); ?>;
                $("#datalogger_id").each(function() {
                    if ($(this).text() == $selected.id)
                        $(this).attr("selected","selected");
                });

                var $selected = <?php echo json_encode($sensors); ?>;
                $('#sensor_id').multiSelect('select', $selected);
            });

            $("#basin_id").change(function() {
                $.getJSON("../../dropdowns/stations/" + $("#basin_id").val(), function(data) {
                    var $stations = $("#station_id");
                    $stations.empty();
                    $.each(data, function(index, value) {
                        $stations.append('<option value="' + index +'">' + value + '</option>');
                    });
                    $("#station_id").trigger("change");
                });
            });

            $("#station_id").change(function() {
                $.getJSON("../../dropdowns/dataloggers/" + $("#station_id").val(), function(data) {
                    var $dataloggers = $("#datalogger_id");
                    $dataloggers.empty();
                    $.each(data, function(index, value) {
                        $dataloggers.append('<option value="' + index +'">' + value + '</option>');
                    });
                    $("#datalogger_id").trigger("change");
                });
            });

            $("#datalogger_id").change(function() {
                $.getJSON("../../dropdowns/sensors/" + $("#datalogger_id").val(), function(data) {
                    var $sensors = $("#sensor_id");
                    $sensors.empty();
                    $.each(data, function(index, value) {
                        $sensors.multiSelect('addOption', { value: index, text: value });
                    });
                    $sensors.multiSelect('refresh');
                });
            });
        });
    </script>

As you can see is a drop down list. I want to retrieve data from database cause is an edit form, all works except 'select' method, doesn't select anything (I tried 'select_all' and didn't work too). Am I doing something wrong?

P.D: I debugged script and values of $sensors wasn't wrong.

chrisvoo commented 9 years ago

Do you mean this peace of code?

var $selected = <?php echo json_encode($sensors); ?>;
$('#sensor_id').multiSelect('select', $selected);

Are you sure that $selected is a string or an array? Only this types are accepted by select method.

mrssam commented 9 years ago

hi sir , can you help me please i want to add values and txet from database by using $("#Order_No").load("DB-includes/fetch_campings.php?checkid=1"); and it stay empty and this my php code

function get_locations(){
$sql = "SELECT LocationID ,title from locations where Is_active=1 LIMIT 0 , 200"; $result = db_query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { // echo ""; i comment this line echo ""; } } else {

 echo "<option value=''>لايوجد مواقع </option>";

} }