1

我是 html 的新手并试图在数据库之间建立关系。我想用数据库填充框

我有一个类似的代码

<div class="content" data-role="content" id="content" >
         <div id="car">
            <select name="selectCar" class="span12" id="Options" >
            <option value="-1">Bir istasyon seçiniz.</option>
            <option value="1">Mimarlık</option>
            <option value="2">Yurtlar</option>
            <option value="3">Bilgisayar Mühendisliği</option>
            <option value="4">Kimya Mühendisliği</option>
            <option value="5">Rektörlük</option>
            </select>           
         </div>
         <div id="cinfo"></div>

        <button onclick="javascript:callCarInfo.call(this,document.getElementById('Options').value);">Call Podcar</button>

      </div>

这里

            <option value="1">Mimarlık</option>
            <option value="2">Yurtlar</option>
            <option value="3">Bilgisayar Mühendisliği</option>
            <option value="4">Kimya Mühendisliği</option>
            <option value="5">Rektörlük</option>

我想用数据库填充这些值和站点名称

我也有一个站功能是

function getStationList()
{
    $db = new DBManager();
    $mysqli = $db->db_connect();

    $query = "SELECT stationId FROM Station";

    $stmt = $mysqli->prepare($query);
    $stmt->execute();
    $result = $stmt->get_result();

    $list = array();

    while($row = $result->fetch_assoc())
    {
        $list[] = $row['stationId'];
    }

    return json_encode($list);
}

我尝试使用 ajax 功能,但我做不到

$.ajax({
    url:'Stations.php',
    type:'POST',
    data: 'q=' + str,
    dataType: 'json',
    success: function( json ) {
        $.each(json, function(i, value) {
            $('#myselect').append($('<option>').text(value).attr('value', value));
        });
    }
});
4

1 回答 1

2

JS(页面.html):

$(document).ready(function() {
  $.ajax({
    url:'Stations.php',
    type:'POST',
    data: 'q=' + str, // not sure you need that as you are not filtering on server side
    dataType: 'json',
    success: function( json ) {
        $.each(json, function(i, value) {
            $('#myselect')
                  .append($('<option></option>', {text:value})
                  .attr('value', text);
        });
    }
  });
})

HTML (page.html):

<div class="content" data-role="content" id="content" >
         <div id="car">
            <select name="selectCar" class="span12" id="Options" ></select>           
         </div>
         <div id="cinfo"></div>
        <button onclick="javascript:callCarInfo.call(this,document.getElementById('Options').value);">Call Podcar</button>
</div>

站.php:

<?php
getStationList();
?>

快速解释:当 page.html 被加载并准备好时,它对 Stations.php 进行 ajax 调用以获取 Stations 列表。在响应时,它会迭代结果并将每个结果的选项元素添加到 ID 为“选项”的选择元素中。

于 2013-05-08T21:39:14.737 回答