Only my checkboxes that are checked are being put into $_POST. I would like for the ones that are checked to be in $_POST with a value of 'true' and the non checked ones to have a value of 'false.' Is there anyway to do this or will the POST request always only contain info about checked checkboxes.
The only thing I can think of is to use hidden form fields and just have the checkboxes manipulate those.
See Posting Unchecked Checkbox and Unchecked checkbox values
It is possible by adding hidden fields with same name.
you can use
<input type="checkbox" name="myCheckbox" value="true" />
<input type="hidden" name="myCheckbox" value="false" />
and then parse the input on the server. You'll either get false or true false, the former maps to false and the latter maps to true
it will only contain content of checked checkboxes. you have to emulate that otherwise by using a form helper or an input filter that wires your variables / arrays the way you like it.
such functionality will need all checkboxes you use per form and can then set to true whats found and leave at false whats not detected.
It's not possible, since it's browser dependend.
You can just check if it's posted by doing
<?php
if (isset($_POST['checkbox1']))
{
// it's posted
}
Or use !:
<?php
if (!isset($_POST['checkbox1']))
{
// it's NOT posted
}
You can assume in your server-side that those who are not set on the post data, are unchecked. Try to verify if the variable is set:
$_POST['a_checkbox'] = (isset($_POST['a_checkbox'])) ? 1 : 0;
Use true and false, or data ase needed.
you could from your php do:
if(isset($_POST['checkbox'])||$_POST['checkbox']!="yes"){
//other code here
}
or, you could have some javascript to add a hidden input before submit for checkbox values that are not being sent
Related
I have a checkbox in my form which looks like this:
<input class="form-control" type="checkbox" id="showCTA" name="showCTA" <?php echo $block['showCTA'] ? 'checked' : ''; ?> />
Everything works fine with this mark up....unless the PHP value equals 1(already checked). If this is the case, I can check and uncheck the box in the from end visually, but the actual html attribute does not change resulting in the same value of 1 being saved to my database on submit.
How can I work around this in a clean manner? I assume the issue is since the PHP value is absolute until submitted, it means the condition around my "checked" attribute is also absolute, therefore I cannot change the attribute.
If the checkbox is not checked and you post the form, the $_POST['showCTA'] will be undefined. So you should use the isset($_POST['showCTA']) method which will return true if the checkbox is checked and if not, false.
I want to pass the values of selected checkboxs between pages. How can I do this ? I think that I have to use JavaScript to get select checkbox values but I don't know how pass to another page the values.
Best Regards
It depends on on which side you want to preserve them. On client side? If so, use session. On clien side? Then use any local storage, like jStorage, HTML5 Storage, DOM Storage etc...
The simplest way would be to use a PHP session:
<?php
//start session
session_start();
//get the posted values from your form
$_SESSION['checkbox_value_1'] = $_POST['checkbox_1_name'];
$_SESSION['checkbox_value_2'] = $_POST['checkbox_2_name'];
$_SESSION['checkbox_value_3'] = $_POST['checkbox_3_name'];
?>
So, when you need the values of the checkboxes you can can get the from the session:
<input type="checkbox" value="<?php echo $_SESSION['checkbox_value_1'] ?>" name="checkbox_1" />
That should do it.
You can also set the checkbox to 'checked' if you add this bit:
<?php
if (isset($_SESSION['checkbox_value_1']) && $_SESSION['checkbox_value_1'] != ''){
//is selected (has a value)
$checkbox_selected = 'checked="checked"';
} else $checkbox_selected = ''; //not checked
?>
Then you can write your checkbox like so:
<input type="checkbox" value="<?php echo $_SESSION['checkbox_value_1'] ?>" name="checkbox_1" <?php echo $checkbox_selected; ?> />
This will add the checked="checked" attribute to the checkbox.
You could also store these in an array if you want to limit the number of session values. Let me know if you'd like an example of that.
you could add get parameter to the form as you post it (so it sets get parameters to the url) representing the checkboxes and then using php to print checked and unchecked boxes according to the last states
for this you would have to catch the submit event of your form via javascript, parse the values of your form's boxes into get-values (many javascript frameworks have support for such operations) and then finally post it to the server, the php on the server then uses the global $_GET variable to test each state when printing the boxes.
(this is a rough idea of an implementation, allways consider unvalidated user parameters could be harmfull for your application)
<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
This works fine, however when you click on submit, and the checkbox is ticked, it passes BOTH the hidden value and the original checkbox value to the $_POST variable in php, can this be avoided?
I have the hidden value there, so that unticked checkboxes are passed to the $_POST variable as well as the ticked ones.
The better approach is to remove the hidden field, and simply have a check in PHP:
if ($_POST['check_box_1']=='1') { /*Do something for ticked*/ }
else { /*Do something for unticked*/ }
You shouldn't need the hidden field. You should in fact not trust any of the form fields sent in the first place. What this means is that you cannot make code which takes the sent fields and trust them to send the correct data (which I assume you do now).
What you should do is to handle all fields you expect to get. That way if you don't get the checkbox value you can still handle that as if it was unticked. Then you also get the added inherent feature of throwing away form data you don't expect in the first place.
No, it will pass all the form data, whatever it is. The right way to do this is not to set the checkbox via a hidden field but to set the checkbox with whatever its state actually is!
I mean... why are you adding the hidden field to begin with?
Your PHP is receiving two fields named check_box_1, and last time I checked there was no way to guarantee that the params would get read into the REQUEST hash in the exact same order as you sent them, so, there's no way to tell which one will arrive last (that's the one whose value will get set). So... this is not the right approach to whatever problem you're trying to solve here.
Welcome to Stack, btw! If you find answers useful or helpful, make sure to mark them as correct and vote them up.
That's normal.
They must be both type="checkbox" to pass only 1 value.
If you want to get only 1 in any cases you can do:
<input type="checkbox" style="display:none;" name="check_box_1" value="0">
Make sure the first input field is of type Checkbox, or else it won't behave like one.
<input type="checkbox" name="check_box_0" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
Everything is working normal with your code so far.
I'm assuming you are creating the hidden field so that 0 is passed to the server when the checkbox is not checked. The problem is that they both get passed when the check box is checked.
As Death said, the way you should be doing it is with a single checkbox and then checking if the value has been sent to the server or not. That's just how checkboxes work.
If you want to have a default set then you will have to handle all that on the server side based on weather the checkbox has a value.
For example:
$myValue = "";
if(isset($_POST['check_box_1']))
{
$myValue=$_POST['check_box_1'];
}
else
{
$myValue="0";
}
I am pulling data down from a MySQL table and loading it into a form for editing(updating) a record. Everything is working great until I come the the check boxes. The checkboxes in the form accurately reflect the values in the appropriate columns in the db. But when the person editing changes the checkbox in the edit form it does not pass the data to the database. I have read a ton of checkbox Q&A on stack overflow but don't seem to find what I am looking for. Sorry if this is a redundant Question. Here is the code.
<label for="amenities-beach">
<input class="choose" name="amenitiesB" id="amenities-beach" type="checkbox"
value="<?php echo $row1["amenitiesB"]; ?>"
<?php echo $row1["amenitiesB"] ? 'checked="checked"' : ''; ?> />
Close to Beach</label>
Where amenitiesB in:
value="<?php echo $row1["amenitiesB"]; ?>
is what has been returned from the DB with a SELECT statement with:
$row1 = mysql_fetch_array($result);
But when I change the value in the form and submit it nothing is passed to the variable in the UPDATE statement. Any idea what I am missing? I have 6 of these checkboxes,amenitiesB, amenitiesK, amenitiesS, amenitiesP, amenitiesF, and preferred all with the same code. Any help would be appreciated.
Thank You,
Dave
Ok here is the code: Everything else in the form updates fine. I attempt to pass it to:
$amenitiesB = $_POST['amenitiesB'];
then I put it into the update statement
Hotels.amenitiesB='".$amenitiesB."',
My UPDATE statement is,
$query="UPDATE Hotels
JOIN surfcup_Rates ON Hotels.id = surfcup_Rates.hotelid
SET Hotels.hotel='".$hotel."',
More columns, then
Hotels.amenitiesB='".$amenitiesB."',
Hotels.amenitiesB='".$amenitiesK."',
Hotels.amenitiesB='".$amenitiesS."',
Hotels.amenitiesB='".$amenitiesP."',
Hotels.amenitiesB='".$amenitiesF."',
Hotels.amenitiesB='".$preferred."',
More columns then:
WHERE Hotels.id='".$id."'";
The problem you have comes because when a checkbox is unchecked, by default its data is not transmitted to your PHP, and that's why you have problems by having the UPDATE query parameter empty.
So before your update statement you should have:
$fieldenabled=(bool)(isset($_POST['CHECKBOXNAME']) ? TRUE : FALSE;
And call your UPDATE query with that.
EDIT: Of course you can change $_POST with $_GET depending on the sending method of the <form>
Edit: I think I get the problem. When the box is initially unchecked, the input has an empty value, then when you check it, it passes an empty value in... it will never fill with what you intend the checked value to be. So, instead you need something like this:
<input class="choose" name="amenitiesB" id="amenities-beach" type="checkbox" value="amenity B Selected" <?php echo $row1["amenitiesB"] ? 'checked="checked"' : ''; ?> />
... don't make the "value" attribute dynamic, or else once it becomes empty it will always be empty.
Original Answer:
I assume when you say "change the value in the form" you mean that you uncheck the checkbox... unchecked checkboxes never send any data when you submit the form. You check for "unchecked" status by checking to see if the form variable has been passed at all.
For example:
if (isset($_GET['amenitiesB'])) {
// process with the knowledge that "amenitiesB" was checked
}
else {
// process with the knowledge that "amenitiesB" was unchecked
}
If you mean that you somehow dynamically change the "value" of the checkbox to something else, then I'll need to see the code that accomplishes that.
The main purpose of the "value" attribute in a checkbox input is when you're passing the variable as an array:
<label for="amenities-beach">
<input class="choose" name="amenities[]" id="amenities-beach" type="checkbox" value="<?php echo $row1["amenitiesB"]; ?>" <?php echo $row1["amenitiesB"] ? 'checked="checked"' : ''; ?> />
Close to Beach
</label>
... note specifically that I've changed the "name" attribute from "amenitiesB" to "amenities[]", which, if carried through all of your amenities checkboxes, will give you access to them all in your processing script through the $_GET['amenities'] array (or $_POST, if applicable). Otherwise, there's not much reason to use the "value" attribute of the checkbox input, because you can get all you need just by knowing $_GET['amenitiesB'] is checked (and thus sent with the form) or unchecked (and not sent with the form).
So, on my form I have two input methods, a text field and a checkbox.
How would I use PHP to test if the checkbox has been checked or not? Perhaps using $_POST?
I want to check if the checkbox is checked, and if it is, set a boolean to true. I have no problems setting variables, but I can't seem to figure out how to get the input from the checkbox....
So, how would I get the input from a checkbox?
In your HTML form you have something similar:
<form method="post">
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
</form>
That is a checkbox named vehicle Now, in your PHP, you would access that with:
$boolean_variable = isset( $_POST['vehicle'] );
Have a look here: http://www.homeandlearn.co.uk/php/php4p11.html
When checkboxes aren't selected, they effectively have no value, so you test it with isset.
Yes, you can using the name attribute of the input when getting the posted form values.
Here is an example:
Handling Checkbox in PHP
<input type="checkbox" name="foo" value="1" />
if (isset($_POST['foo'])) {
// checkbox was checked
}
Checked checkboxes and their value are submitted in a POST request. Unchecked checkboxes aren't submitted. So if $_POST['foo'] exists at all, it was checked.