Laravel checkbox is not submitted to controller - php

I have a simple form in a view like this:
<form action="{{ URL::route('admin.x') }}" method="POST">
<input type="text" value="b" name="title" />
<input type="text" value="c" name="type" />
<input type="text" value="d" name="postfix" />
<input type="checkbox" name="check" value="ss" />
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
I used
dd(Input::all());
for checking all of values posted to controller from view. the result is like below and I want to know how to post checkbox value with form.
array:4 [▼
"title" => ""
"type" => ""
"postfix" => ""
"_token" => "kSM3pO11KOOWQcCx4PeWcbi4r4AsFx0rSGQoVFtG"
]

A checkbox input is not submitted to the server side script if not checked by the user or by a client side script. So, in these cases, the input isn't sent to your controller
In laravel, if you want to 'normalize' somehow the situation when the input isn't sent, you can create a field by yourself:
$data = Input::all();
if ( ! isset($data['check']) )
$data['check'] = false;

You can ensure that your server side receives a value for the checkbox, regardless of whether it was checked, by creating a hidden input field with the same name above the checkbox field.
This way, if the form is submitted without the checkbox being checked, the server side will receive the value from the hidden input field whereas if the checkbox is checked the server will receive the checked value due to the field being lower in the form.
<input type="hidden" name="checkbox" value="0"/>
<input type="checkbox" name="checkbox" value="ss"/>

This returns true if the checkbox is checked, false if not.
Input:has('check')

Related

Repopulate radio buttons on failed validation (CodeIgniter)

I'm having a problem with CodeIgniter repopulating a form after validation fails.
HTML:
<label for="public">Anyone</label>
<input type="radio" name="target" value="public" <?php echo set_radio('target', 'public', TRUE); ?> />
<label for="direct">Specific</label>
<input type="radio" name="target" value="direct" <?php echo set_radio('target', 'direct'); ?> />
When I first load the form I get this source code:
<label for="public">Anyone</label>
<input type="radio" name="target" value="public" checked="checked" />
<label for="direct">Specific</label>
<input type="radio" name="target" value="direct" />
... so the third parameter is working (the default "TRUE")
But when I submit the form with validation errors on other fields, the form is reloaded with no radio button selected.
A var_dump($_POST) after submitting (with intentional validation errors on other fields) shows this:
array (size=9)
'target' => string 'direct' (length=6)
...
but no radio button is selected.
Got to be something simple... help?
The answer is apparently to add a fake rule to the radio button.
$this->form_validation->set_rules("tenderType", "", "trim");
Thanks to #FuzzyTree pointing to the solution here: stackoverflow.com/q/16473459/3574819

How to store checkbox value as 1 and 0

It doesn't stores value in my database. It always stores "0" value even if i checked the item.
Here is my code:
<input type="checkbox" name="parental" <?php $parental = (isset($_POST['parental'])) ? 0 : 1;?>/>
<input type="hidden" name="parental" />
You forgot the attribute value and also add echo in php code
<input type="checkbox" name="parental" value="<?php echo $parental = (isset($_POST['parental'])) ? 0 : 1;?>" />
also why you have this element?
<input type="hidden" name="parental" />
It will overwrite the checkbox since both have the same name, either remove the hidden element or change the name
To be clear, checkbox value should always be the same, in your case 1, since checkbox passes it's value when it is checked, and does not when it is not. Also, if you want to use hidden field, you should use it before checkbox, so in case checkbox is not checked, it still passes 0.
For example:
<input type="hidden" name="parental" value="0" />
<input type="checkbox" name="parental" value="1" />
If the checkbox is checked $_POST["parental"] will equal 1, else it will equal 0. There are better ways to achieve this in php after the submit, though.
Also, regarding your conditional, I suppose you want to check the checkbox if parental is 1, which you should do like this, for example:
<input type="checkbox" name="parental" value="1" <? echo $_POST["parental"]=="1"?"checked":""; ?> />
Changing input type checkbox value, does not really have much sense in your case, if I understood right.

How to mantain radiobutton, dropdown and checkbox values if submit fails PHP

I know how to it with text inputs. I can easily put a php script in its value, but doing it with input groups seems different. How can I mantain the values of group inputs if the submission of the form fails?
To re-mark a checkbox or radio button as checked, you use this code:
<input type="checkbox" name="foo" id="foo" checked="checked"/>
The key is checked="checked".
If you are using groups of checkboxes, make sure the name of the field ends with brackets [], like this:
<input type="checkbox" name="foo[]" id="foo_1" value="1" checked="checked"/>
<input type="checkbox" name="foo[]" id="foo_2" value="2" checked="checked"/>
Then your $_REQUEST['foo'] variable will automatically be an array of checked values. You can use in_array to see if a particular checkbox was checked.
Update based on comment
Here's how I would set it:
<input type="checkbox" name="foo[]" id="foo_1" value="1" <?= (isset($_POST['foo'] && in_array('1', $_POST['foo'])) ? 'check="checked"' : '' ?>/>
For single items (like radios), use this:
<input type="radio" name="foo" id="foo" value="1" <?= isset($_POST['radio]) ? 'check="checked"' : '' ?>/>
Hope that helps.
Update 2:
Also, make sure you escape user input! Your example should look like this:
<input type="text" name="username" value="<?php if(isset($_POST['username']) echo htmlspecialchars($_POST['username']);?>">
Always assume the user is trying to hack your system, always escape user input!
Print the " checked" attribute for radio buttons and checkbox input tags, or the " selected" attribute for dropdown option tags.

check box with accept terms

I would like to check whether user checked box or not with php code. how can I do this?? and if checked then what kind of value I will get??
<input name="accept" type="checkbox" class="tickbox" value="" />
In this specific case, you will get $_POST['accept'] == '' , which is immensely un-useful.
You'll want to add a value to that tag:
<input name="accept" type="checkbox" class="tickbox" value="1" />
With that value, you'll get $_POST['accept'] == '1' when the checkbox is checked, and no 'accept' key at all when the checkbox is not checked.
<input type="hidden" name="accept" value="0" />
<input type="checkbox" name="accept" value="1" />
if unchecked : return hidden field’s value => 0
if checked : return checkbox’s value => 1
You need to have a value assigned to the checkbox. If the check box is ticked, this value will be returned when the form is submitted. The other option is to use a Javascript to check this before submission.

static value in a form

I have a form with list of fields :
last name
email
country
message
Before sending the form, i want to joint to it a static value : destination=marketing.
i don't want it to be visible in my form. how would this be done ?
Input hidden is what you need :
<input type="hidden" name="destination" value="marketing" />
<input type="hidden" name="destination" value="marketing" />
or you can add this variable when you process the form server-side
With a hidden input. Or put it in the session instead.

Categories