复选框值在数据库中存储空值 [英] checkbox value store null value in database
本文介绍了复选框值在数据库中存储空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将checkbox的值存储在数据库中,第一个checkbox(category)值将保存正确的值,但第二个checkbox(sub_category)值将存储为空值.类别存储为1,2.但是子类别存储了null值,我想将其存储为与1,2,3之类的类别相同.
I store the value of checkbox in the database, the first checkbox(category) value will save correct but second checkbox(sub_category) value stored in the database as null. Category store like 1,2. But subcategory stored null value I want to store the same as a category like 1,2,3.
table_business={id`,`business_id`,`business_name`, `business_pic`,`address`, `email`,`mobile`,`contact_number`,`website`,`business_work_photo`, `category_id`,
`sub_category_id`}
功能
public function insertBusinessDetails(Request $req)
{
$mytime = Carbon::now();
$business_id = DB::table('table_business_registration')->orderby('id', 'DESC')->first();
$created_at = $mytime->toDateTimeString();
$updated_at = $mytime->toDateTimeString();
$name_of_business = $req->input('business_name');
$address = $req->input('address');
$mobile = $req->input('mobile');
$contact_number = $req->input('contact_number');
$category = implode(',', $req->input('category'));
$sub_category = $req->input('sub_category');
$email = $req->input('email');
$website = $req->input('website');
if ($business_id == NULL) {
$query_insert = array(
"business_id" => 1, "business_name" => $name_of_business, "address" => $address, "mobile" => $mobile,
"contact_number" => $contact_number, "email" => $email, "website" => $website, "category_id" => $category,
"sub_category_id" => $sub_category, "updated_at" => $updated_at, "created_at" => $created_at
);
DB::table('table_business_registration')->insert($query_insert);
} else {
$new_business_id = $business_id->business_id + 1;
$query_insert = array(
"business_name" => $name_of_business, "address" => $address, "mobile" => $mobile,
"contact_number" => $contact_number, "email" => $email, "website" => $website, "category_id" => $category,
"sub_category_id" => $sub_category, "updated_at" => $updated_at, "created_at" => $created_at
);
DB::table('table_business_registration')->insert($query_insert);
}
}
刀片
<div class="row mt-3">
<div class="col-sm-6">
<label for="category" class="">Category</label>
</div>
<div class="col-sm-4">
@foreach($category_list as $category)
<div class="form-check form-check-inline">
<input class="form-check-input category" type="checkbox" id="category" name="category[]" value="{{$category->category_id}}">
<label class="form-check-label" for="category" style="float: left;">{{$category->category}}</label>
</div>
@endforeach
</div>
</div>
<div class="row mt-3">
<div class="col-sm-6">
<label for="subcategory" class="">Sub-Category</label>
</div>
<div class="col-sm-4">
<div class="form-check form-check-inline">
<div class="sub_category_of_ multi" id="sub_category" name="sub_category">
</div>
</div>
</div>
</div>
jquery代码
<script type="text/javascript">
$(function () {
$('.category').click(function() {
var id = $(this).attr('value');
if (this.checked) {
if (id) {
$.ajax({
type:"GET",
url: "{{url('getSubCategory')}}?category=" + id,
success: function(res) {
if (res) {
$.each(res, function(key, value) {
$("#sub_category").append('<input type="checkbox" value="' + key + '" class="sub_category_of_'+id+'">');
$("#sub_category").append('<label class="sub_category_of_'+id+'">'+ value + '</label>');
});
} else {
// Handle the error case here without removing an existing elements
}
}
});
} else {
// Handle the error case here
}
}
else
{
// Remove the unchecked category checkbox if available
if (id && $('.sub_category_of_'+id).length) {
$('.sub_category_of_'+id).remove();
}
}
});
});
</script>
推荐答案
您应将'name'属性添加到sub_category复选框,以将sub_category值传递到另一端.
You should add the 'name' attribute to your sub_category check boxes to pass the sub_category value to other end.
用以下代码替换您的代码:
Replace your code with below code:
$(function () {
$('.category').click(function() {
var id = $(this).attr('value');
if (this.checked) {
if (id) {
$.ajax({
type:"GET",
url: "{{url('getSubCategory')}}?category=" + id,
success: function(res) {
if (res) {
$.each(res, function(key, value) {
$("#sub_category").append('<input type="checkbox" value="' + key + '" class="sub_category_of_'+id+'" name="sub_category[]"'); // name attribute added
$("#sub_category").append('<label class="sub_category_of_'+id+'">'+ value + '</label>');
});
} else {
// Handle the error case here without removing an existing elements
}
}
});
} else {
// Handle the error case here
}
}
else
{
// Remove the unchecked category checkbox if available
if (id && $('.sub_category_of_'+id).length) {
$('.sub_category_of_'+id).remove();
}
}
});
});
这篇关于复选框值在数据库中存储空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文