How to remember checkbox state when default state is checked? - php

I have a form with a check box.
The check box has three "states":
Checked as default.
Unchecked by user.
Checked again by user.
How would I do something like that? If I use !isset(), I can uncheck the checkbox but it doesn't distinguish if it was unchecked by the user or if it's suppose to be checked as default.
I want to use this to refill the last values of the form inputs whenever there are form errors on submit.
It's easy to do when the default state of the check box is unchecked. I can then just do:
if (isset($_POST['checkbox'])) echo ' checked'; //check box has been checked

The cleanest way of maintaining the form's state after submission is to use an array to store the input values. First you set the default values, then if the form has been submitted, overwrite the defaults with whatever was submitted. In the HTML form, always output the value from the form array.
// create the array (for both viewing the form and maintaining the submitted values)
$form = array(
'myCheckbox' => true // default to checked
'firstName' => '',
)
// was the form submitted?
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// overwrite the form array
$form['myCheckbox'] = isset($_POST['myCheckbox']);
$form['firstName'] = $_POST['firstName'];
// at this point do any validation, if it fails, let the form show
}
Now for the form:
<form method="post">
<input type="checkbox" name="myCheckbox" value="1" <?php echo $form['myCheckbox'] ? 'checked' : '' ?> />
<input type="text" name="firstName" value="<?php echo htmlspecialchars($form['firstName']); ?>" />
</form>
In the above, I added a text input firstName to illustrate that all of the form inputs should be managed by the array.

you can use javascript with an input hidden to tell how many times the checkbox was clicked
<form action="">
<input id="bike" type="checkbox" name="vehicle" value="Bike" checked onchange="document.getElementById('bike-state').value=parseInt(document.getElementById('bike-state').value)+1">I have a bike
<input type="hidden" id="bike-state" name="count" value="0">
</form>
now in the server side if $_POST['bike-state'] is an odd then it was unchecked, if it is even and greater than 0 it was unchecked and then rechecked , otherwise if still 0 it was checked by default

Related

PHP Set a hook in a checkbox if the value is 1 or uncheck if the value is 0

i hope you can help me, i think the solution is simple, but i am beginner, so kick my brain.
Now i want on a new page that the checkbox will be shown as either checked or unchecked depending on the result.
The results are available as 1 or 0, but how can I now display a checkbox as checked or unchecked?
I'm grateful for every tip.
If it is Javascript you can add your PHP variable to it like so:
echo '<input type="checkbox" name="checkbox1" value="'.$checkboxValue'.">
If you wanted to get the value of the check box you have to submit it in a form like so:
<form action="index.php" method="post">
Checkbox: <input type="checkbox" name="checkbox1">
<input type="submit">
</form>
index.php
<?php
if (isset($_POST['checkbox1']){
$checkboxValue = $_POST['checkbox1'];
}
?>
Note: The $_POST value is from the name attribute of your checkbox

CheckBox Value to Text in DB

I am making a project. How to convert CheckBox tick value to Yes for MySQL database and untick no?
I was able to make simple HTML inputs of Check Boxes. How to set the things above in PHP?
To get the result of the checkbox into a PHP script, submit it as a field in a form either through POST or GET, then in your targeted php script, process the $_POST or $_GET superglobal you chose:
if (isset($_POST['Check']) && !empty($_POST['Check'])) {
$Result = $_POST['Check'];
}
To get the result of whether the checkbox is checked or not into the field you can use a single checkbox, and a hidden field in the form to hold the value "Yes" or "no"
<fieldset>
<form action="check.php" method="post">
<input type="checkbox" checked="true" onclick="getSelected(this)" value="Yes">Query
<input type="hidden" name="Check" id="Status" value="Yes">
<button type="submit">Submit</button>
</form>
</fieldset>
To change the value of the hidden field when the checkbox is checked or unchecked use javascript to set the value of the field:
// Set Hidden Field to Result of Checkbox
function getSelected(elem) {
// Get Hidden Field Reference
var status = document.getElementById("Status");
// Determine if Checked
if(elem.checked) {
status.value = "Yes";
} else {
status.value = "No";
}
}
Then when you submit the form, it is POSTed to the target script and you will have a field "Check" that is either "Yes" or "No". From there you can use PHP to process your variables and then run whatever MySQL queries you need.
Hope this helps.

form submit checkbox value

I have a bike I have a car
I know when the form is submitted the value will be vehicle=Bike&vehicle=Car if both are ticked
Is there a way to make the value to be vehicle=Bike,Car
Put them into one variable then separated in a comma
Since you are using POST and multiple check boxes, set the name of each check box like this:
<input type="checkbox" name="vehicle[]" value="Bike">
<input type="checkbox" name="vehicle[]" value="Car">
Then when your form is submitted you will receive an array of all the checked boxes and their values in the array:
$_POST['vehicle'][];
Now if both boxes are checked you can retrieve the values in a foreach loop:
foreach($_POST['vehicle'] as $type){
echo "Type = ".$type;
}
With this you will get an output of
Type = Bike
Type = Car
Try this one,
<input type="hidden" name="vechiclesStr" id="vechiclesStr">
//on submitting,
document.getElementById("vechiclesStr").value = document.formname.vechicle.join();

not getting values from my post

I have a html form
<form action="process.php" method="post">
<input type="checkbox" name="name[v1]" />
<input type="checkbox" name="name[v2]" />
<input type="checkbox" name="name[v3]" />
<input type="submit" name="update" value="update">
</form>
If they is only one check box is ticked then I only see that check box
Array
(
[\'v3\'] => on
)
If I have checked all three box then I see them all.
Array
(
[\'v1\'] => on
[\'v2\'] => on
[\'v3\'] => on
)
Is they any way I can see all of my checkbox even if they are not checked.
process.php
foreach( $_POST['name'] as $k => $v )
{
echo "key: ".$k;
}
Checkboxes and radio buttons are not passed on to the processing script if they don't have a "checked" attribute set. This is HTML4 by design.
The only way you can set a state is using something like:
if(!isset($_POST['mycheckbox'])){ $_POST['mycheckbox'] = 0; }
or better yet:
$_POST['mycheckbox'] = isset($_POST['checkbox']);
Regarding radio buttons, you should only use the first version since radio buttons can have more than one value so instead of setting a TRUE/FALSE in them, you want to set a default value instead.
Another note, DISABLED elements are not posted, even if they have a value, you will never see them, this is another design feature of HTML4+

displaying all form field names but checkbox's and radios fields dont show

i have an html form full of text fields, checkbox's , and radio fields.
i wanted to get all the names of the fields so that i can get started in validating the information in them.
the method i am using to get them is
if(isset($_POST['submit'])) {
foreach($_POST as $name => $value) {
print $name."<br/>";
}
}
but i noticed that it only displays textbox and textarea field names and it doesnt include checkbox and radio field names through this submission. do i need to include anything for it to grab the field names of those?
Checkboxes and radio buttons work a little differently than your standard inputs. If a checkbox is present on a form that doesn't necessarily mean that it will be available in the resulting POST information. Rather, those values will only be avialable if they are actually marked (checkboxes checked and radio buttons selected). The proper way to test for their value in PHP is not to check the field value but rather to check isset() first.
For a checkbox:
$data['my_checkbox'] = isset($_POST['my_checkbox']) ? 'on' : 'off';
and for a radio button:
$data['my_radio'] = isset($_POST['my_radio']) ? $_POST['my_radio'] : false;
To be a little more descriptive let's say you have the following form:
<form action="test.php" method="post">
<input type="text" name="email" value="" />
<input type="checkbox" name="active" value="Yes" />
<input type="submit" value="Submit" />
</form>
If I were to submit that form with an email value of 'test#email.com' but not check the checkbox I would have the following in $_POST:
Array (
'email' => 'test#email.com'
)
However, if I were to submit the same form with the same email address and check the checkbox I would have the following:
Array (
'email' => 'test#email.com',
'active' => 'Yes'
)
Hope that helps.
0./ Try using the following code to see the raw posted data:
echo '<pre>';
print_r($_POST);
echo '</pre>';
1./ Make sure you use a name attribute value for your checkbox and radio inputs.
Typically for checkboxes, it will be an array.
<input type="checkbox" id"=fruit-apple" name="fruits[]" value="apple" />
<input type="checkbox" id="fruit-pear" name="fruits[]" value="pears" />
2./ Make sure they sit inside the form tag.
3./ If you submit using a javascript call, try disabling javascript and see if the error stays. If it does not, you know your javascript is the culprit.

Categories