【发布时间】:2017-04-24 17:33:41
【问题描述】:
我想将复选框值插入数据库,当我单击特定复选框时,必须插入相应的值,但对我来说,之前选中的复选框值也再次插入到数据库中,我该如何克服这个问题?
如果我选中了复选框 1,再次插入到数据库中,我选中了复选框 2,我的表格显示了 checkbox1、checkbox1、checkbox2。
在这里我使用了 foreach 循环它的问题或任何其他方法来解决这个问题。
JS 代码:
$("input[type=checkbox]").on("change", function() {
var ids = [];
var dis = [];
$('input[type=checkbox]:checked').each(function() {
ids.push($(this).val());
});
$('input[type=checkbox]:not(:checked)').each(function() {
dis.push($(this).val());
});
dis = dis.toString();
ids = ids.toString();
$.ajax({
url: "bodcheckbox.php",
type: "POST",
async: true,
cache: false,
data: ({
value: ids,
unchecked: dis,
}),
dataType: "text",
success: function(data) {
alert("activitysubmitted");
}
});
});
PHP 代码:
session_start();
include 'config.php';
$username = $_SESSION['username'];
//if(isset($_POST['value']))
//{
//unchecked box value--
$unchecked = $_POST['unchecked'];
$uncheckboxvalue = array();
$uncheckboxvalue = explode(",", $unchecked);
//--checkedbox value--
$checkbox = array();
$checklist = $_POST['value'];
$checkbox = explode(",", $checklist);
$currentdate = date('d-m-Y');
$year = date('Y', strtotime($currentdate));
$month = date('F', strtotime($currentdate));
$date = date('d', strtotime($currentdate));
date_default_timezone_set('Asia/Kolkata');
$currenttime = date("h-i-sa");
if (!empty($checklist)) {
foreach ($checkbox as $check) {
$bod_insert = mysql_query("insert into bod(username,activityname,tickeddate,tickedtime,status)values('$username','" . mysql_real_escape_string($check) . "','$date-$month-$year','$currenttime','yes')", $conn);
}
foreach ($uncheckboxvalue as $uncheck) {
$bod_update = mysql_query("update bod set status='no' where activityname='" . mysql_real_escape_string($uncheck) . "' and username='$username'", $conn);
}
}
【问题讨论】:
-
你想只插入当前选中元素的值吗?
-
yes madalin only current checkbox value
标签: jquery