check box with accept terms - php

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.

Related

Laravel checkbox is not submitted to controller

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')

Required fields in multiple checkbox and radio button

In html for radio button I have this:
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female <br />
and for check list:
<input name="checkboxvar[]" type="checkbox" value="dog" />Dog</td>
<input name="checkboxvar[]" type="checkbox" value="cat" />Cat</td>
<input name="checkboxvar[]" type="checkbox" value="rabbit" />Rabbit</td>
For the radio buttons how can I make them required and for the checkbox to take the result if the user select more than 1 values?
For the previous fields I use a submit button
<input type="submit" name="submit" id="submit" value="submit" />
by using required attribute as follows:-
<input type="radio" name="sex" value="male" required />Male
For making a radio button required, use the required tag (you only need add the required tag for one radio input per group):
<input type="radio" name="sex" value="male" required/> Male<br />
The required attribute is shorthand for required="required" or required="true" (the fact that its there constitutes a 'truthy' value, as does any (nonempty) string).
For making at least one of the checkboxes required: An answer has already been provided here; this basically states that, given a <button> element with and id of checkBtn, the following code can help (with some of my comments on it):
$(document).ready(function () {
// grabbing the button's element and adding an
// onclick event to it
$('#checkBtn').click(function() {
// grabs all elements which are checkboxes and
// are checked; if none are checked, the length
// is zero; else, the length is a 'truthy' value
checked = $("input[type=checkbox]:checked").length;
// if no elements are checked
if(!checked) {
// error handling
alert("You much check at least one checkbox.") return false;
}
});
});
Assuming that you want to detect the error upon the submit button's click; if you want to handle the error otherwise, you might want to clarify that (your answer is a tad vague).
Hope it helps!

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.

PHP POST data showing as 'ON' instead of numerical 1

I am posting a value from a radio button , its value is 1 , but when I echo var_dump($_POST) , then it shows as "on" instead of 1 . Please help. I need to get 1 instead of "on".
<input type="radio" id = "item" value="<?php echo $ItemID[0]; ?>"
name="ItemID"><label for="ItemID"><?php echo $ItemIDName[0]; ?></label>
BROWSER GENERATED HTML
<input type="radio" id = "ITEMA"
value="1" name="ITEM"><label for="ITEMA">A</label>
<input type="radio" id = "ITEMB" value="2" checked="checked" name="ITEM" ><label for="ITEMB">B</label>
<input type="radio" id = "ITEMC" value="3" name="ITEM" >
<label for="ITEMC">C</label>
The default value for radio button is "on" ! Check your php code $ItemID[0] if it's really being set to 1 !
You need to set the value for the radio button to something like value="1", it will only be used when checked
The value setting defines what will be submitted if checked.

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.

Categories