PHP / jQuery Ajax奇怪的结果,需要一些建议吗?

时间:2014-04-18 16:56:44

标签: javascript php jquery ajax

我有一个问题,我无法理解。 我有一个让我们称之为“插入页面”的用户输入名字,姓氏,并有一个“楼层”列表的下拉列表。现在每层都有工作站。因此,当用户选择楼层时,我会进行Ajax调用以恢复该楼层的工作站:

Insert Floor Ajax:

 $('#floor').change(function (){
    if ($('#floor option:selected').text() == "Please select a floor"){
        $('#workstation').slideUp();
    } else {
        $('#workstation').slideDown();
    }
    $floor = $('#floor option:selected').val();
    $.ajax({
        url: "workstationAjax.php",
        data: {floor: $floor},
        type: 'post',
        dataType: 'json',
        success: function(data){
            $.each(data, function (i, msg) {
                $('#workstation').append("<option id='department' name='department' value=" + msg.floor_id + ">" + msg.description + "</option>")
            });
        }
    });
});

WorkstationAjax.php

<?php

header ("content-type: application/json; charset=utf-8");
require_once '../Config/db.php';

if (!isset($_POST['floor']) || $_POST['floor'] == "") {
    return false;
} else {

$floorID = $_POST['floor'];
$query = $conn->query("select * from workstation where floor_id= $floorID");
$query->execute();

$array = $query->fetchAll(PDO::FETCH_ASSOC);


echo json_encode($array);

}

所以它返回正确的'工作站'但是当从下拉列表中选择工作站选项时,它会将每个选项附加到下拉列表中的第一个选项,我无法弄清楚为什么......

HTML

        <h3><strong>Insert Employee</strong></h3>
    <form id="insertEmployee" action="admin/insert.php" method="post">
        <input type="text" name="firstname" placeholder="Enter Firstname" class="form-control-static"/>
        <p/>
        <input type="text" name="lastname" placeholder="Enter Lastname"  class="form-control-static" />
        <br />

        <select id="floor" name="floor">
            <option value="">Please select a floor</option>
            <?php
            $floorArray = getData("floor");
            foreach ($floorArray as $key=> $row) {
                echo "<option id='floor' name='floor' value=\"{$row['floor_id']}\">{$row['description']}</option>";
            }

            ?>

        </select>
<br>
        <select id="workstation" anme="workstation">

        </select>
 <br />
        <select id="department" name="department">


        </select>

编辑: 我修好了。这是一个非常愚蠢的错误,但不确定它是如何修复的......将不得不进一步审视。在插入层Ajax我改变了: 从:

            $.each(data, function (i, msg) {
            $('#workstation').append("<option id='department' name='department' value=" + msg.floor_id + ">" + msg.description + "</option>")
        });

要:

            $.each(data, function (i, msg) {
            $('#workstation').append("<option id='workstation' name='workstation' value=" + msg.floor_id + ">" + msg.description + "</option>")
        });

0 个答案:

没有答案