Php 如何根据复选框I'将整数添加到数组中;你点击了吗?

Php 如何根据复选框I'将整数添加到数组中;你点击了吗?,php,laravel,Php,Laravel,我在我的Laravel项目中使用了完整的日历,我需要在其中添加重复事件 我在UI中添加了代表一周中每一天(周一、周二、周三等)的复选框 在完整日历中,我添加了属性daysofweek 基本上: 一周中重复事件的天数(是一个整数数组) 每个整数表示一周中的一天,0表示星期天,1表示星期一,等等。。。(例如:[2,4]表示每周二和周四重复一次事件) 如果省略,则假定事件每天重复 我想在星期一单击,一个整数将添加到数组中,如下例所示: $event_dow=[1] 如果在星期一和星期二同时单击,阵

我在我的Laravel项目中使用了完整的日历,我需要在其中添加重复事件

我在UI中添加了代表一周中每一天(周一、周二、周三等)的复选框

在完整日历中,我添加了属性
daysofweek

基本上:

  • 一周中重复事件的天数(是一个整数数组)

  • 每个整数表示一周中的一天,0表示星期天,1表示星期一,等等。。。(例如:[2,4]表示每周二和周四重复一次事件)

  • 如果省略,则假定事件每天重复

我想在星期一单击,一个整数将添加到数组中,如下例所示:

$event_dow=[1]

如果在星期一和星期二同时单击,阵列将具有:

$event_dow=[1,2]

我不知道怎么做

请遵循以下我的代码:

EventController.php

public function store(Request $request)
    {

        //

       $this->validate($request, [
        'event_name' => 'required|string|min:2|max:255',
        'daterange' => 'required',
    ]);
       $time_start = $request->input('start_time');

       $time_end = $request->input('end_time');

       $tempDate = $request->input('daterange');
       $temp2 = str_replace('-', null, $tempDate);
       $temp2 = str_replace('/', '-', $temp2);
       $temp3 = explode('  ', $temp2);

       $date1 = date_create_from_format('m-d-Y', $temp3[0]);
       $date2 = date_create_from_format('m-d-Y', $temp3[1]);

       $event_dow = [];

       if($request->has('monCheck'))
       {
        $$event_dow = $event_dow.'1';
    };

    if($request->has('tueCheck'))
    {
        $event_dow = $event_dow.'2';
    };

    if($request->has('wedCheck'))
    {
        $event_dow = $event_dow.'3';
    };

    if($request->has('thurCheck'))
    {
        $event_dow = $event_dow.'4';
    };

    if($request->has('friCheck'))
    {
        $event_dow = $event_dow.'5';
    };

    if($request->has('satCheck'))
    {
        $event_dow = $event_dow.'6';
    };

    if($request->has('sunCheck'))
    {
        $event_dow = $event_dow.'7';
    };

    $event = new event([
        'event_name' => $request->input('event_name'),
        'event_desc' => $request->input('event_desc'),
        'event_venue' => $request->input('event_venue'),
        'event_start' => date_format($date1, 'Y-m-d H:i:s'),
        'event_finish' => date_format($date2, 'Y-m-d H:i:s'),
        'event_venue' => $request->input('event_venue'),
            'event_dow' => $request->input('event_dow'), //days of week
            'evcat_id' => $request->input('evcat_id'),
            'user_id' => $request->input('user_id'),


        ]);

    $event->save();
    Session::flash('success', 'Event Created Successfully');
    return redirect()->route('event.sched');

}

刃锉

<div class="row" style="margin-left: 50px;">
                        <div class="form-check form-check-inline">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" name="monCheck" id="monCheck">
                                <label class="custom-control-label" for="monCheck">Mon</label>
                            </div>
                        </div>


                        <div class="form-check form-check-inline">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" name="tueCheck" id="tueCheck">
                                <label class="custom-control-label" for="tueCheck">Tue</label>
                            </div>
                        </div>

                        <div class="form-check form-check-inline">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" name="wedCheck" id="wedCheck">
                                <label class="custom-control-label" for="wedCheck">Wed</label>
                            </div>
                        </div>

                        <div class="form-check form-check-inline">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" name="thurCheck" id="thurCheck">
                                <label class="custom-control-label" for="thurCheck">Th</label>
                            </div>
                        </div>

                        <div class="form-check form-check-inline">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" name="friCheck" id="friCheck">
                                <label class="custom-control-label" for="friCheck">Fri</label>
                            </div>
                        </div>

                        <div class="form-check form-check-inline">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" name="satCheck" id="satCheck">
                                <label class="custom-control-label" for="satCheck">Sat</label>
                            </div>
                        </div>

                        <div class="form-check form-check-inline">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" name="sunCheck" id="sunCheck">
                                <label class="custom-control-label" for="sunCheck">Sun</label>
                            </div>
                        </div>
                    </div>

周一
星期二
结婚
Th
星期五
坐
太阳

您可以通过为每个复选框添加值来解决此问题

<div class="row" style="margin-left: 50px;">
  <div class="form-check form-check-inline">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input" name="monCheck" id="monCheck" value="1">
      <label class="custom-control-label" for="monCheck">Mon</label>
    </div>
  </div>


  <div class="form-check form-check-inline">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input" name="tueCheck" id="tueCheck" value="2">
      <label class="custom-control-label" for="tueCheck">Tue</label>
    </div>
  </div>

  <div class="form-check form-check-inline">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input" name="wedCheck" id="wedCheck" value="3">
      <label class="custom-control-label" for="wedCheck">Wed</label>
    </div>
  </div>

  <div class="form-check form-check-inline">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input" name="thurCheck" id="thurCheck" value="4">
      <label class="custom-control-label" for="thurCheck">Th</label>
    </div>
  </div>

  <div class="form-check form-check-inline">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input" name="friCheck" id="friCheck" value="5">
      <label class="custom-control-label" for="friCheck">Fri</label>
    </div>
  </div>

  <div class="form-check form-check-inline">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input" name="satCheck" id="satCheck" value="6">
      <label class="custom-control-label" for="satCheck">Sat</label>
    </div>
  </div>

  <div class="form-check form-check-inline">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input" name="sunCheck" id="sunCheck" value="0">
      <label class="custom-control-label" for="sunCheck">Sun</label>
    </div>
  </div>
</div>
或者您也可以使用jQuery来解决它。试试这个

首先添加按钮以提交请求

保存
脚本:


$(“#提交”)。单击(函数(){
let check=$('.custom控件输入:checked');
设eventDow=[];
检查每个功能(i){
eventDow[i]=$(this.val()
});
console.log(eventDow);//返回数字数组
});
  $event_dow = [];

       if($request->has('monCheck'))
       {
        $event_dow[] = $request->monCheck;
    };

    if($request->has('tueCheck'))
    {
        $event_dow[] = $request->tueCheck;
    };

    if($request->has('wedCheck'))
    {
        $event_dow[] = $request->wedCheck;
    };

    if($request->has('thurCheck'))
    {
        $event_dow[] = $request->thurCheck;
    };

    if($request->has('friCheck'))
    {
        $event_dow[] = $request->friCheck;
    };

    if($request->has('satCheck'))
    {
        $event_dow[] = $request->satCheck;
    };

    if($request->has('sunCheck'))
    {
        $event_dow[] = $request->sunCheck;
    };

<button id="submit" type="button">Save</button>
<script>
  $('#submit').click(function() {
    let check = $('.custom-control-input:checked');
    let eventDow = [];

  check.each(function(i) {
    eventDow[i] = $(this).val()
  });
   console.log(eventDow); // returns array of numbers 
  });
</script>