我无法从mysql结果中填充第二个下拉列表。据我所知,代码似乎很好但显然有些东西不对。
该值正在PHP中从$rtvcompany = $_GET['rtvcompany'];
正确传递到loadboxRtvaddr.php?rtvcompany=
,如果我在mysql中运行查询,它将返回正确的数据。
如果有人可以请检查我的代码并指出我的错误,我将不胜感激。非常感谢
已解决:if语句中缺少}
。杜!
HTML
<div class="form-group">
<label class="labelStyle" for="rtvdept">Department</label> <select class="form-control chosen-select" data-placeholder="Select a Dept..." id="rtvdept" name="rtvdept">
<option value="">
</option>
</select>
<div id="deptmessage"></div>
<div class="servicesHelp">
Please select a department where your box is stored for retrieval.
</div>
</div>
JS Change事件
$(function() {
$("#rtvcompany").change(function() {
$(this).after('<div id="loader"><img src="/logistor.new/admin/images/loader.gif" alt="loading files" /></div>');
$.get('/domain/admin/requests/boxes/retrieve/loadboxRtvaddr.php?rtvcompany=' + $(this).val(), function(data) {
$("#rtvdept").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
$("#rtvdept").trigger("chosen:updated");
});
});
});
});
PHP
<?php session_start(); ?>
<?php include ($_SERVER['DOCUMENT_ROOT'] . '/domain/admin/Connections/domain.php'); ?>
<?php
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$rtvcompany = $_GET['rtvcompany'];
$sqladdr = "SELECT * FROM departments WHERE code = '".$rtvcompany."'";
$resultaddr = mysqli_query($conn, $sqladdr) or die(mysqli_error());
if (mysqli_num_rows($resultaddr) > 0) {
while ($row_addr = mysqli_fetch_array($resultaddr)) {
echo "<option value='$row_addr[name]'>$row_addr[name]</option>";
}
mysqli_close($conn);
?>
答案 0 :(得分:1)
如下所示更改jquery代码。
$(document).on('#rtvcompany','change',function() {
$(this).after('<div id="loader"><img src="/logistor.new/admin/images/loader.gif" alt="loading files" /></div>');
$.get('/domain/admin/requests/boxes/retrieve/loadboxRtvaddr.php?rtvcompany=' + $(this).val(), function(data) {
$("#rtvdept").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
$("#rtvdept").trigger("chosen:updated");
});
}); });