我正在运行一个AJAX函数,该函数将数据发送到运行MySQL查询并显示结果的页面。
此按钮单击启动功能
<div class="text-center">
<button type="button" class="btn btn-sm btn-success" onclick="detailsmodal(<?php echo $product['id']; ?>)">Details</button>
</div>
这是AJAX功能
function detailsmodal(id){
var data = {"id" : id};
$.ajax({
url : '/php-ecom/includes/details-modal.php',
method : "post",
data : data,
success : function(data){
$('body').append(data);
$('#details-modal').modal('toggle');
},
error : function(){
alert('something went wrong');
}
});
};
这是查询和显示数据库的URL
<?php
require_once '../core/init.php';
$id = $_POST['id'];
$id = (int)$id;
$sql = "SELECT * FROM products WHERE id = '$id' ";
$result = $db->query($sql);
$product = mysqli_fetch_assoc($result);
$brand_id = $product['brand'];
$sql2 = " SELECT brand FROM brand WHERE id = '$brand_id' ";
$brand_query = $db->query($sql);
$brand = mysqli_fetch_assoc($brand_query);
$sizestring = $product['sizes'];
$size_array = explode(',', $sizestring);
?>
<?php ob_start(); ?>
<div class="modal fade details-1" id="details-modal" tabindex="-1" role="dialog" aria-labelledby="details-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button class="close" type="button" onclick = "closeModal()" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title text-center"><?php echo $product['title']; ?></h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<span id="modal_errors" class="bg-danger"></span>
<div class="col-sm-6">
<div class="center-block">
<img src="<?php echo $product['image']; ?>" alt="<?php echo $product['title']; ?>" class="details img-responsive">
</div>
</div>
<div class="col-sm-6">
<h4>Details</h4>
<p><?php echo $product['description']; ?></p>
<hr>
<p>Price: <?php echo $product['list_price']; ?></p>
<p>Brand: <?php echo $brand['brand']; ?></p>
<form action="add_cart.php" method="post" id="add_product_form">
<input type="hidden" name="product_id" value="<?=$id; ?>">
<input type="hidden" name="available" id="available" value="">
<div class="form-group">
<div class="col-xs-3">
<label for="quantity">Quantity:</label>
<input type="number" class="form-control" id="quantity" name="quantity">
</div>
<p>Available: 3</p>
</div><br>
<div class="form-group">
<label for="size"></label>
<select name="size" id="size" class="form-control">
<option value="">options</option>
<?php foreach($size_array as $string){
$string_array = explode(':',$string);
$size = $string_array[0];
$available = $string_array[1];
?>
<option value="<?php echo $size; ?>" data-available="<?=$available ?>"><?php echo $size; ?> (Available: <?php echo $available; ?>)</option>
<?php }; ?>
</select>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-default" onclick="closeModal()">Close</button>
<button class="btn btn-warning" onclick="add_to_cart(); return false;"><span class="glyphicon glyphicon-shopping-cart"></span> Add To Cart </button>
</div>
</div>
</div>
</div>
<script>
$('#size').change(function(){
var available = $('#size option:selected').data('available');
$('#available').val(available);
});
function closeModal(){
$('#details-modal').modal('hide');
setTimeout(function(){
$('#details').remove();
$('.modal-backdrop').remove();
},500);
};
</script>
<?php echo ob_get_clean(); ?>
问题在于即使我发送不同的数据,我也得到相同的MySQL结果。