我正在ajax中使用成功函数,直到我在数据库表中添加了新行并且成功函数仅出于某些原因(不是因为gender_id)才返回该行,一切都进行得很好,这是我的ajax函数:
function editProduct(productId = null) {
if (productId) {
$("#productId").remove();
// remove text-error
$(".text-danger").remove();
// remove from-group error
$(".form-group")
.removeClass("has-error")
.removeClass("has-success");
// modal spinner
$(".div-loading").removeClass("div-hide");
// modal div
$(".div-result").addClass("div-hide");
$.ajax({
url: "php_action/fetchSelectedProduct.php",
type: "post",
data: { productId: productId },
dataType: "json",
success: function(response) {
// alert(response.product_image);
// modal spinner
$(".div-loading").addClass("div-hide");
// modal div
$(".div-result").removeClass("div-hide");
$("#getProductImage").attr("src", "stock/" + response.product_image);
$("#editProductImage").fileinput({});
// product id
$(".editProductFooter").append(
'<input type="hidden" name="productId" id="productId" value="' +
response.product_id +
'" />'
);
$(".editProductPhotoFooter").append(
'<input type="hidden" name="productId" id="productId" value="' +
response.product_id +
'" />'
);
// product name
$("#editProductName").val(response.product_name);
// quantity
$("#editQuantity").val(response.quantity);
// rate
$("#editRate").val(response.price);
// brand name
$("#editBrandName").val(response.brand_id);
// category name
$("#editCategoryName").val(response.categories_id);
$("#editGender").val(response.gender_id);
// status
$("#editProductStatus").val(response.active);
// update the product data function
$("#editProductForm")
.unbind("submit")
.bind("submit", function() {
// form validation
var productImage = $("#editProductImage").val();
var productName = $("#editProductName").val();
var quantity = $("#editQuantity").val();
var rate = $("#editRate").val();
var brandName = $("#editBrandName").val();
var categoryName = $("#editCategoryName").val();
var genderName = $("#editGender").val();
var productStatus = $("#editProductStatus").val();
if (productName == "") {
$("#editProductName").after(
'<p class="text-danger">Product Name field is required</p>'
);
$("#editProductName")
.closest(".form-group")
.addClass("has-error");
} else {
// remov error text field
$("#editProductName")
.find(".text-danger")
.remove();
// success out for form
$("#editProductName")
.closest(".form-group")
.addClass("has-success");
} // /else
if (quantity == "") {
$("#editQuantity").after(
'<p class="text-danger">Quantity field is required</p>'
);
$("#editQuantity")
.closest(".form-group")
.addClass("has-error");
} else {
// remov error text field
$("#editQuantity")
.find(".text-danger")
.remove();
// success out for form
$("#editQuantity")
.closest(".form-group")
.addClass("has-success");
} // /else
if (rate == "") {
$("#editRate").after(
'<p class="text-danger">Rate field is required</p>'
);
$("#editRate")
.closest(".form-group")
.addClass("has-error");
} else {
// remov error text field
$("#editRate")
.find(".text-danger")
.remove();
// success out for form
$("#editRate")
.closest(".form-group")
.addClass("has-success");
} // /else
if (brandName == "") {
$("#editBrandName").after(
'<p class="text-danger">Brand Name field is required</p>'
);
$("#editBrandName")
.closest(".form-group")
.addClass("has-error");
} else {
// remov error text field
$("#editBrandName")
.find(".text-danger")
.remove();
// success out for form
$("#editBrandName")
.closest(".form-group")
.addClass("has-success");
} // /else
if (categoryName == "") {
$("#editCategoryName").after(
'<p class="text-danger">Category Name field is required</p>'
);
$("#editCategoryName")
.closest(".form-group")
.addClass("has-error");
} else {
// remov error text field
$("#editCategoryName")
.find(".text-danger")
.remove();
// success out for form
$("#editCategoryName")
.closest(".form-group")
.addClass("has-success");
} // /else
if (genderName == "") {
$("#editGender").after(
'<p class="text-danger">Category Name field is required</p>'
);
$("#editGender")
.closest(".form-group")
.addClass("has-error");
} else {
// remov error text field
$("#editGender")
.find(".text-danger")
.remove();
// success out for form
$("#editGender")
.closest(".form-group")
.addClass("has-success");
}
if (productStatus == "") {
$("#editProductStatus").after(
'<p class="text-danger">Product Status field is required</p>'
);
$("#editProductStatus")
.closest(".form-group")
.addClass("has-error");
} else {
// remov error text field
$("#editProductStatus")
.find(".text-danger")
.remove();
// success out for form
$("#editProductStatus")
.closest(".form-group")
.addClass("has-success");
} // /else
if (
productName &&
quantity &&
rate &&
brandName &&
genderName &&
categoryName &&
productStatus
) {
// submit loading button
$("#editProductBtn").button("loading");
var form = $(this);
var formData = new FormData(this);
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: formData,
dataType: "json",
cache: false,
contentType: false,
processData: false,
success: function(response) {
console.log(response);
if (response.success == true) {
// submit loading button
$("#editProductBtn").button("reset");
$(
"html, body, div.modal, div.modal-content, div.modal-body"
).animate({ scrollTop: "0" }, 100);
// shows a successful message after operation
$("#edit-product-messages").html(
'<div class="alert alert-success">' +
'<button type="button" class="close" data-dismiss="alert">×</button>' +
'<strong><i class="glyphicon glyphicon-ok-sign"></i></strong> ' +
response.messages +
"</div>"
);
// remove the mesages
$(".alert-success")
.delay(500)
.show(10, function() {
$(this)
.delay(3000)
.hide(10, function() {
$(this).remove();
});
}); // /.alert
// reload the manage student table
manageProductTable.ajax.reload(null, true);
// remove text-error
$(".text-danger").remove();
// remove from-group error
$(".form-group")
.removeClass("has-error")
.removeClass("has-success");
} // /if response.success
}
// /success function
}); // /ajax function
} // /if validation is ok
return false;
}); // update the product data function
// update the product image
$("#updateProductImageForm")
.unbind("submit")
.bind("submit", function() {
// form validation
var productImage = $("#editProductImage").val();
if (productImage == "") {
$("#editProductImage")
.closest(".center-block")
.after(
'<p class="text-danger">Product Image field is required</p>'
);
$("#editProductImage")
.closest(".form-group")
.addClass("has-error");
} else {
// remov error text field
$("#editProductImage")
.find(".text-danger")
.remove();
// success out for form
$("#editProductImage")
.closest(".form-group")
.addClass("has-success");
} // /else
if (productImage) {
// submit loading button
$("#editProductImageBtn").button("loading");
var form = $(this);
var formData = new FormData(this);
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: formData,
dataType: "json",
cache: false,
contentType: false,
processData: false,
success: function(response) {
if (response.success == true) {
// submit loading button
$("#editProductImageBtn").button("reset");
$(
"html, body, div.modal, div.modal-content, div.modal-body"
).animate({ scrollTop: "0" }, 100);
// shows a successful message after operation
$("#edit-productPhoto-messages").html(
'<div class="alert alert-success">' +
'<button type="button" class="close" data-dismiss="alert">×</button>' +
'<strong><i class="glyphicon glyphicon-ok-sign"></i></strong> ' +
response.messages +
"</div>"
);
// remove the mesages
$(".alert-success")
.delay(500)
.show(10, function() {
$(this)
.delay(3000)
.hide(10, function() {
$(this).remove();
});
}); // /.alert
// reload the manage student table
manageProductTable.ajax.reload(null, true);
$(".fileinput-remove-button").click();
$.ajax({
url: "php_action/fetchProductImageUrl.php?i=" + productId,
type: "post",
success: function(response) {
$("#getProductImage").attr("src", response);
}
});
// remove text-error
$(".text-danger").remove();
// remove from-group error
$(".form-group")
.removeClass("has-error")
.removeClass("has-success");
} // /if response.success
} // /success function
}); // /ajax function
} // /if validation is ok
return false;
}); // /update the product image
} // /success function
}); // /ajax to fetch product image
} else {
alert("error please refresh the page");
}
} // /edit product function
,这是我用来从数据库中获取数据的sql查询fetchSelectedProduct
:
<?php
require_once 'core.php';
$productId = $_POST['productId'];
$sql = "SELECT product_id, product_name, product_image, brand_id, categories_id, quantity, price, active, status, gender_id FROM product WHERE product_id = $productId";
$result = $connect->query($sql);
if($result->num_rows > 0) {
$row = $result->fetch_array();
} // if num_rows
$connect->close();
echo json_encode($row);
有人知道我的代码有什么问题吗?我试图修复它,但无法完成。
答案 0 :(得分:-1)
几乎可以肯定是ajax缓存,因此只需添加:
cache: false
呼叫,或向请求uri添加新参数:
'?t=' + Date.now();