0

in my html i can able to add more fields using jquery and the field am generating is check box array. i need to retrieve this in my controller.

so far i have tried

 <input type="checkbox" value="1" name="washing[]"  id="washing[]">
 <input type="checkbox" value="1" name="dryclean[]" id="dryclean[]">

In controller

 $clothtypeid = $request->input('clothtypeid');
 $washing = $request->input('washing');
 $pressing = $request->input('pressing');

 for($i=0;$i<count($clothtypeid);$i++){
   //in here how to test if the checkbox is ticked then value=1 else 0 ??
 }



I expect if the checkbox is ticked then i have to make 1 else 0
Thanks in Advance
Kumar
  • 21
  • 3

1 Answers1

0

So I did a bit of digging and found this Tutorial which suggests;

  • Setting the input type to "checkbox" like you did there..
  • Keep the same name and include the '[]' at the end of the name attribute so that you have the following in your form;

    <input type="checkbox" name="check_list[]" value="washing">
    <input type="checkbox" name="check_list[]" value="dryclean">
    

Then in the controller you can get the value by doing the following;

if(!empty($request['check_list'])){
 // Loop through array to get the checked values.
  foreach($request['check_list'] as $item){
  //do something here like;
    if($item == 'washing'){ //make db operation or assign to a variable..}
    else if($item == 'dryclean'){//make db operation}
  }
}

This is a very basic example as you can see and I have to say that I haven't tested it yet but I believe it will work. Try it and give feedback.

EDIT: There is an answer here

Edgar
  • 472
  • 6
  • 17
  • i appreciate your reply.i already found the answer. in controller $addmore = $request->input('addmore'); $maxkey = max(array_keys($addmore)); for($i=0; $i<= $maxkey; $i++){ if(isset($addmore[$i]['washing']) && $addmore[$i]['washing'] === "1"){ $newwash = 1; } else { $newwash = 0; } } – Kumar May 13 '19 at 04:03
  • glad you found a solution :) – Edgar May 13 '19 at 05:14