调用JS函数时,从数据库中填充下拉菜单的值?

时间:2017-10-27 15:33:33

标签: javascript jquery mysql

与此小提琴相似:http://jsfiddle.net/vigneshmoha/bbxMe/2/

<html>
<head>
<script>
function dateGenerate() {
       var date = new Date(), dateArray = new Array(), i;
       curYear = date.getFullYear();
        for(i = 0; i<5; i++) {
            dateArray[i] = curYear+i;
        }
        return dateArray;
}
// The Above function will create an array of five consecutive year from the the current year.

function addSelect(divname) {
    var newDiv=document.createElement('div');
    var html = '<select>', dates = dateGenerate(), i;
    for(i = 0; i < dates.length; i++) {
        html += "<option value='"+dates[i]+"'>"+dates[i]+"</option>";
    }
    html += '</select>';
    newDiv.innerHTML= html;
    document.getElementById(divname).appendChild(newDiv);
}

</script>

</head>

<body>

<div id="select-container">
</div>
<button id="add" onclick="addSelect('select-container');">Add Dropdown</button>

</body>
</html>

除了我有一个带有记录的MySql数据库,我想用下面的(在这种情况下为First和Last名称)填充下拉列表。有没有人有什么资源或示例可以实现这一目标?我被告知AJAX可以在这里提供帮助,但我对这种语言有点不熟悉。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

var e = document.getElementById("YourDrodownboxVale");
var _id = e.options[e.selectedIndex].value;

/ *这是从数据库* /

发布一些数据
$.ajax({
            url: uploadURI,
            type: 'post',
            data: {id: _id},
            success: function () {
                alert('Susccess');
            }
        });

/ *这是从数据库* /

获取一些数据
    $.ajax({
            url: uploadURI,
            type: 'get',
            data: {id:_id},
            success: function (data) {
                //put the resulting data hear
                document.getElementById("YourDivID").html = data;
            }
        });

//这是另一种方法。用于打开数据库和创建,将数据插入表中的函数

getSelectedRow = function(val)
{
    db.transaction(function(transaction) {
          transaction.executeSql('SELECT * FROM Employ where number = ' + parseInt(val, 10) + ';',[], selectedRowValues, errorHandler);

    });
};
selectedRowValues = function(transaction,results)
{
     for(var i = 0; i < results.rows.length; i++)
     {
         var row = results.rows.item(i);
         alert(row['number']);
         alert(row['name']);                 
     }
};