我有一個清單,用戶需要檢查多個複選框,然後我應該計算每個複選框的總價格並顯示它。問題是我應該將選中的複選框插入數據庫。所以當我使用isset()函數時,我有一個錯誤。所以我想用jQuery來代替,並將檢查的ID發送到數據庫,但我不知道該怎麼做。發送選中的複選框ID到數據庫使用jQuery的
<form method="post" action="#">
<div class="agileinfo_services_grids">
<?php
$sql = "SELECT * FROM services";
$result = mysqli_query($connection, $sql);
$num_rows = mysqli_num_rows($result);
if($num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$price = $row['price'];
$id = $row['id'];
echo "<div class='col-md-4 agileinfo_services_grid'>
<div class='agileinfo_services_grid1'>
<div class='price'><h3>$" . $row['price'] . "</h3></div>
<h4>" . $row['title'] . "</h4>
<p>" . $row['details'] . "</p>
<div class='agileinfo_services_grid1_pos'>
<input id='$id' name:'checkbox[]' class='icon-checkbox my-activity' type='checkbox' value='$price'/>
<label for='$id'>
<span class='glyphicon glyphicon-unchecked unchecked'></span>
<span class='glyphicon glyphicon-check checked'></span>
</label>
</div>
</div>
</div>";
}
} else {
echo "No Current Services Available";
}
?>
</div>
<div class="total">
<div class="total-left">
<p>Total :<span>$<input type="text" id="amount" readonly></span>
</div>
<div class="total-right">
<input type="submit" value="Check Out" name="submit">
</div>
<div class="clear"> </div>
</div>
</form>
下面是我用來計算複選框總價格的函數。我怎樣才能修改它發送到數據庫的檢查框的ID。
<script type="text/javascript">
$(document).ready(function() {
$(".my-activity").click(function(event) {
var total = 0;
$(".my-activity:checked").each(function() {
total += parseInt($(this).val());
$.post("services.php", {id: this.id, checked:this.checked});
});
if (total == 0) {
$('#amount').val('');
} else {
$('#amount').val(total);
}
});
});
</script>
歡迎SO。請訪問[幫助],看看有什麼和如何問。建議:發佈努力和代碼。在這種情況下[google搜索您的標題](https://www.google.com/search?q=send+checkbox+to+php+jquery) – mplungjan