复选框值将空值存储在数据库中

时间:2019-06-21 12:48:48

标签: jquery laravel

我将checkbox的值存储在数据库中,第一个checkbox(category)值将保存正确的值,但将第二个checkbox(sub_category)值存储为null。类别存储如1,2。但是子类别存储了空值,我想将其存储为与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>

2 个答案:

答案 0 :(得分:1)

您应将“名称”属性添加到sub_category复选框,以将sub_category值传递到另一端。

用以下代码替换您的代码:

$(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();
      }
    }
  });
});

答案 1 :(得分:0)

您的代码有误。您应该使用Laravel Relationships在数据库中存储有关类别和与此相关的信息。然后,无需ajax的额外请求即可呈现类别。 关于字段sub_category中的内容不为空,您不应该阻止此内容,因为如果您的类别中没有sub_category类别,则可能会出现问题,但是如果需要,可以将默认值添加到datebase 关于表table_business_registration,您不应该在字段business_id上通过php进行增量,您可以在数据库中进行此自动增量。