我试图在代码中调用ajax查询,但它不起作用。我有公共页眉和页脚(php)为所有其他文件。它返回所需的数据,但显示在html代码中。我到处找了找,但都找不到。我真的很抱歉没有在这里发布代码,因为我尝试了一个小时发布代码,但我不能,因为这个编辑器给出了一个关于未格式化代码的错误。
代码的Html部分
<div class="form-group">
<select class="selectpicker" data-live-search="true" data-style="form-control"
name="" id="sale_customer" required>
</select>
</div>
<div class="row cus-note">
<p class="note-field col-6" id="note_customer" name="search"> </p>
<textarea type="text" class="form-control note-field col-6" name="" value=""
placeholder="New Note..." required></textarea>
</div>
JQuery部分
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
function loadCustomer(notes, cust) {
// console.log("cust");
$.ajax({
url: "<?php echo $folder;?>crud.php",
type: "POST",
data: {
note: notes,
customer: cust
},
success: function(data) {
if (data.note == "notes") {
// alert('haaalp');
$("#note_customer").html(data);
} else {
$("#sale_customer").append(data);
alert('haaalp');
}
}
});
}
loadCustomer("noteD","");
$("#sale_customer").on("change", function() {
var customer = $("#sale_customer").val();
console.log(customer);
if (customer != "") {
loadCustomer("notes", customer);
}
else {
$("#note_customer").html("");
}
});
});
</script>
服务器端代码
if($_POST['note'] == "noteD"){
$sql = "SELECT * FROM customer";
$query = mysqli_query($db, $sql) or die("Query Unsuccessful.");
$sale_customer = "<option selected value='wic'><sub>test</sub></option>";
while($row = mysqli_fetch_assoc($query)){
$sale_customer .= "<option value='{$row['name']}'>{$row['name']}</option>";
}
echo $sale_customer;
}
else if($_POST['note'] == "notes"){
$customer = $_POST['customer'];
$query = mysqli_query($db, "SELECT * FROM customer WHERE name = 'Ali'") or die("Query
Unsuccessful.");
$sale_customer = "";
while($row = mysqli_fetch_assoc($query)){
$sale_customer .= "{$row['note']}";
}
echo $sale_customer;
}
我可以在控制台中看到成功返回的数据。它也是龚的内部条件,但不显示在选择器。在if条件中,如果use only note代替Data.note
会引发未定义note
的异常。
成功返回的Ajax代码
控制台上返回的数据
数据.备注改为仅备注
找不到便笺对象
好啊!!!
在四处寻找之后,我找到了一个解决这个问题的方法。主要问题是引导程序SelectPicker。ajax返回了数据,但select没有显示它。在
success: function(data) {
if (note == "notes") {
$("#note_customer").html(data);
} else {
$("#sale_customer").append(data);
}
}
我刚刚添加了$(“#sale_customer”).append(data).selectpicker('refresh');
它工作了,但另一个问题仍然存在,即在中没有定义if(note==“notes”){
“note
”。为此,我在ajax外部将两个变量作为var note=notes;
和var customer=cust;
发布在数据对象中,作为
data: {
note,
customer
},
结果出现了意料之中的结果。
问题出在引导程序selectpicker和数据对象上。