简体   繁体   English

从数据库值中选择下拉菜单默认选择

[英]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. 您好,我有一个下拉菜单,选择该菜单时会执行Ajax / JavaScript将选定的值放入数据库中。我想下次使用菜单打开页面时,选择drop dowm选项,该选项将从数据库值。 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.: 您可以以编程方式构建html并回显单个块,从而可以在创建所选值时对其进行测试:

$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. 好吧,这确实很容易做到,但是这取决于表中的哪一列定义了是否应在下拉菜单中将该项设置为selected-default。 So here: 所以在这里:

Before you render your code do a select statement on your table to select all options for the drop down list. 在呈现代码之前,在表上执行select语句以选择下拉列表的所有选项。 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: 假设您的表有一个selected (boolean)的列selected (boolean) ,它告诉我们是否应将表条目设置为默认值,从这里开始,这很简单:

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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