简体   繁体   中英

Drop down menu default selection to be selected from database value

Hello I have a drop down menu, which on selection executes Ajax/JavaScript to put the selected value inside the database.I would like the next time when I open the page with the menu the drop dowm option which is selected to be pulled from the database value. This way the user will know what is selected inside the database.

here is the code of my drop dowm menu:

<?php echo "<select name='status' id='$id' idc='$idc'>" ?>
  <option value="">Option:</option>
  <option value="val1">Val1</option>
  <option value="Val2">Val2</option>
  </select>

the function that is storing the information inside the database from the menu is:

<script>
$(document).ready(function(e) {
$('select[name=status]').change(function(){

        selectstatus = $("select[name=status]").val();  
        var id = $(this).attr('id');
        var idc = $(this).attr('idc');
        $.ajax({
        type: "POST",
        url: "selectbackend.php",
        data: {"selectstatus": selectstatus, "id": id, "idc": idc
       },
        })

        .fail(function(jqXHR, textStatus, errorThrown){alert(jqXHR+"--"+textStatus+"--"+errorThrown);});
});//end change
});//end ready
</script>

and this is the conection with the database for storing the information from the drop down menu:

<?php
$selectstatus = $_POST['selectstatus'];
$id = $_POST['id'];
$idc = $_POST['idc'];

$host = "localhost";
$user = "user";
$password = "pass";
$dbname = "test";

$cxn = mysqli_connect($host,$user,$password,$dbname);
if (mysqli_connect_errno()) {echo "No connection" . mysqli_connect_error();}

    $query = " UPDATE subscriptions
           SET status = '$selectstatus'
           WHERE user_id='$idc' AND curso_id='$id'";

$result = mysqli_query($cxn, $query) or die ("could not query database 1");
?>

You could build up your html programmatically and echo out a single block, allowing you to test for the selected value as you create it.:

$option_html = '';
$options = array( 
    'val1' => 'Value 1',
    'val2' => 'Value 2'
 );

foreach( $options as $key => $value ) {
    // is_selected should be a function to determine if value is
    // the value stored in the db for this particular case
    $selected = is_selected( $key ) ? 'selected' : '';
    $option_html .= "<option {$selected} value=\"{$key}\">{$value}</option>";
}

If you find yourself doing this kind of thing a lot, a templating engine can be a very useful tool.

Well this is really easy to do however it depends what column in your table defines if item should be set as selected-default in the dropdown. So here:

Before you render your code do a select statement on your table to select all options for the drop down list. Lets assume your table has a column selected (boolean) which tells us if the table entry should be set as default and from here on it's very trivial:

echo "<select name='status' id='$id' idc='$idc'>";
// Loop over all options
foreach($results as $option){

    // If option has value selected set as true then append selected="selected" to it.
    if($option->selected)
    {
        echo '<option selected="selected" value="' . $option->value . '">' . $option->value . '</option>'; 
    } else {
        echo '<option value="' . $option->value . '">' . $option->value . '</option>';
    }
}
</select>

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