1

This is my html blade code

{{Form::checkbox('remember_me', '', array('id'=>'remember_id'))}}
        <label for="remember_id">Remember me</label>

This is my controller code:

 echo Input::get('remember_me');exit;
  1. The result is always empty, why please?

  2. The checkbox is always checked when I run the page, why please?

Thanks

4 Answers 4

5

Please have a look on the [parameter list of the Form::checkbox() method][1].

The second parameter is your checkbox value. You manually set it to an empty string. Set it to null in order to keep the browsers default values (laravel default is 1). The third parameter is a boolean. Set it to true to check the box and false to uncheck it.

The fourth parameter is your options array where you can specify your id. So the correct method call should be:

{{Form::checkbox('remember_me', null, false, array('id'=>'remember_id'))}} 

Update:

Checkboxes that are not checked, will not be included in your POST data. So the only reliable way to verify that a checkbox has been checked is to check if it is set. That can be done using isset() with regular PHP functions, or if laravel is being used, by using Input::has() which returns a boolean dependent on whether your input data contains a given key.

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

4 Comments

when applying your code, the checkbox becomes unchecked, which is good, but the controller print nothing when the checkbox is unchecked and printed null when the checkbox is checked
Checkboxes that are not checked will not be send via POST, that's why you get null when it's unchecked and you cannot alter this behavior. That means you first need to check whether the checkbox has a value, and then check for the value. null should not be printed when you checked the box. Are you sure you did not put null in string literals?
when i removed the string literal arround the null, i got on when the checkbox is checked and nothing when it is not checked. so you mean that I need just to check if the form has the checkbox name. otherwise, the checkbox will not checked, right?
Right. As I said checkboxes that are not checked will not be sent via POST and so their value will be null. You can verify that by using Input::has('remember_me') in your controller. You should get null when the checkbox has not been checked and true otherwise
1

You did not add a value to the checkbox

{{Form::checkbox('remember_me', 'value goes here', true, array('id'=>'remember_id'))}}

The second param is the value

5 Comments

I already tried to put false in the second value, but still the checkbox is checked and nothing printed in the controller
edited my answer, forgot that the third param is the checked state, try now
now when the checkbox is unchecked, the controller prints nothing, and when the checkbox is checked the controller prints value goes here . what i need is either true/false or on/of
Its normal, you will only get a value from a checkbox, when its checked, and where it says "value goes here" i just placed that there as an example, replace it with your value...
+1 to you, I wish I could accept more than answer. Many thanks for your great efforts
0

Normally I write the checkbox without blade and I can do with it whatever I want, like normal HTML. I don't see why you can do it the normal HTML way, because it always ends up doing the same thing you expert.

2 Comments

+1 to your answer, I choose blade over html because I need to save the checkbox value when the page is redirected in case the authentication falls
Actually, you can do that with a normal checkbox, blade has nothing to with that, second, and no offense, blade generates a checkbox for you, and it helps you generate a cleaner html.
0
{!! Form::label('Test-2') !!} {!! Form::checkbox('ch[]', 'value-2', false); !!} 

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.