2

I created checkboxes in form using javascript:

<input type="checkbox" name="is_ok[]" />
<input type="checkbox" name="is_ok[]" />
<input type="checkbox" name="is_ok[]" />

When I check 1st and 3rd checkbox and submit the form, Input::get("is_ok") returns me:

['on', 'on']

Is there any way to get value as ['on', null, 'on'] or ['on', 'off', 'on']?

Thanks in advance.

5
  • weill i use laravel collective and set it to default 0 and if clicked then 1 Commented Apr 13, 2017 at 13:08
  • you need to set a value attribute. When the checkbox is checked the name/value pair will be sent to the server otherwise it won't be. Unchecked values don't get sent by default. Commented Apr 13, 2017 at 13:16
  • @apokryfos when I add value like this <input type="checkbox" name="is_ok[]" value="1" /> I get ["1","1"]. Unchecked values couldn't pass and I don't know which checkbox is unchecked. Commented Apr 13, 2017 at 13:23
  • 1
    Then give each checkbox a different value so you can differentiate. There may be a jQuery solution when listening to the submit event of the form but there's no reason to add this complexity. Commented Apr 13, 2017 at 13:24
  • @apokryfos It is not exact solution but acceptable. thanks. Commented Apr 13, 2017 at 13:28

5 Answers 5

3

Hey assign some values to checkboxes like user_id, product_id etc what ever in your application.

E.g. View

<input type="checkbox" name="is_ok[]" value="1" />
<input type="checkbox" name="is_ok[]" value="2" />
<input type="checkbox" name="is_ok[]" value="3" />

E.g. Controller

<?php
    if(isset($_POST['is_ok'])){
        if (is_array($_POST['is_ok'])) {
             foreach($_POST['is_ok'] as $value){
                echo $value;
             }
          } else {
            $value = $_POST['is_ok'];
            echo $value;
       }
   }
?>

You will get array of selected checkbox.

Hope it helps..

Sign up to request clarification or add additional context in comments.

2 Comments

this is unnecessary IMO!
Same implementation as @apokryfos suggested you. At least you will get required checkboxes
2

I think I have a "good" solution to this (kind of).

<input type="checkbox" name="is_ok[0]" />
<input type="checkbox" name="is_ok[1]" />
<input type="checkbox" name="is_ok[2]" />

(Forced indices here)

In the request:

$array = \Request::get("is_ok") + array_fill(0,3,0);
ksort($array);

This will ensure that (a) The checkbox indices are maintained as expected. (b) the gaps are filled when the request is received.

It's sloppy but may work.

Comments

0

IMHO this is the best practice:

In your migration set that db table field to boolean and default 0

$table->boolean->('is_ok')->default(0);

{!! Form::checkbox('is_ok[]',  false,  isset($model->checkbox) ? : 0) !!}

and if you are not using laravel collective for forms then you can use vanilla php

 <input type="checkbox" name="is_ok[]" value="<?php isset($model->checkbox) ? : 0; ?>" />

Comments

0

My solution is this for laravel 5

$request->get('is_ok[]');

Comments

0

Though it might not be best practice, here is what I did first of all I send id of a specific model as a value.

<input type="checkbox" id="verify" name="is_varified[]" value="{{$bank->id}}" {{$bank->is_varified == 1 ? 'checked':''}}>

And in controller I added two query to update the field.

//handaling the issue of checkbox
Bank::where("user_id",$user->id)->whereIn('id',$request->is_varified)->update(['is_varified'=> 1]);
Bank::where("user_id",$user->id)->whereNotIn('id',$request->is_varified)->update(['is_varified'=> 0]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.