vendredi 11 août 2023

Multiple Checkboxes with same name insert checked value in database

I am making copy of checkboxes , I want to insert first checkboxes checked values in one row then second checkboxes checked values in second row in database , likewise

This is my form code (same i am making copy of these divs)

   
                  <div class="row">
                      <label class="col-md-2 col-form-label" for="basic-icon-default-fullname">Validation</label>
                      <div class="col-md-10">
                          <div class="form-check mb-2">
                              <input class="form-check-input" type="checkbox" value="required" id="required" class="validation" name="validation[]" checked />
                              <label class="form-check-label" for="required">Required</label>
                          </div>
                          <div class="form-check mb-2">
                              <input class="form-check-input" type="checkbox" value="alpha" id="alpha" class="validation" name="validation[]" />
                              <label class="form-check-label" for="alpha">Alphabets Only</label>
                          </div>
                          <div class="form-check mb-2">
                              <input class="form-check-input" type="checkbox" value="/^[a-zA-Z\s]+$/" id="alphaSpace" class="validation" name="validation[]" />
                              <label class="form-check-label" for="alphaSpace">Alphabets with Space</label>
                          </div>
                          <div class="form-check mb-2">
                              <input class="form-check-input" type="checkbox" value="number" id="number" class="validation" name="validation[]" />
                              <label class="form-check-label" for="number">Number Only</label>
                          </div>
                      </div>
                  </div>

I have tried like this way but it is not working as per my requirement

This is my controller code

$customInputNames = $request->input('customInputName');
        $inputTypes = $request->input('inputType');
        $validationsArrays = $request->input('validation'); // Array of arrays containing checked validation values
        $count = count($customInputNames);

        for ($i = 0; $i < $count; $i++) {
            $customInput = new InputForm();
            $customInput->customInputName = $customInputNames[$i];
            $customInput->inputType = $inputTypes[$i];

            $validationArray = $validationsArrays[$i]; // Get the array of checked validation values for the current input

            // Split the string into an array, remove empty values, and rejoin
            $validationArray = implode(',', array_filter(explode(',', $validationArray)));

            $customInput->validation = $validationArray; // Updated validation string
            $customInput->eventid = $eventId;
            $customInput->save();
        }



Aucun commentaire:

Enregistrer un commentaire