Update MySQL DB with PHP from Checkbox - php

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

Related

Setting checkbox to checked using PHP is results in no way to clear the checked attribute after render

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.

Return Checkbox states: Form with errors: PHP

I created a from with multiple required fields that the users my complete. Then I also have 3 checkboxes of which the user must select at least one of them. When the user submit the form I start doing the checks and each error I find I store in an array. If there are any errors I display the errors for the user plus the form with the values that the user already entered. This bit work fine. I want to know how I would be able to return a checked checkbox if the user checked any of then and any amount, but none, fo them? With from fields it's easy:
<input type="text" name="First_Name" value="<?php echo $First_Name; ?>" />
My question is in what way I would be able to get the result of my checkbox the user select? My guess is that it might be something like:
if(isset())
But I am not sure. Any help on this please?
<input type="checkbox" name="check1" value="1"<?php if (isset($_POST['check1'])) echo " checked"; ?>>
Try if(isset($_POST['checkbox'])) { do this }
If the checkbox has been selected the if statement will run.
When you submit a checkbox, the "value" that has been given will be set. So if your value gets set into a database this probably will be a tinyint(1)
you should use the following:
<input type="text" name="aCheckBox" value="1" <?php if (#$_POST['aCheckBox']=="1") { echo 'checked="checked"';}?> />
The #-sign is supressing the notices, so you dont have to use isset. Isset is useless anyway, because some browsers send the field in the post array but without value. So the isset would ways return true.

Passing checkbox state to PHP

<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";
}

Retain the value of while() generated list of checkbox fields. Value="checked" (but value has data already?)

I'm in a bit of an odd spot here. I am modifying a script, and my validation is all working fine with the exception of my checkbox input selections which are generated from an array with a while loop..
What I need to be able to do is retain the value="checked" if the box has been selected, but the value field currently stores the id value to be passed to a table, and I the value option is how the "checked" is called... So
...PHP_SELF...
while(...)
{
<input name="seminar[]" type="checkbox" id="seminar[]" value="<?= $data[id] ?>">
}
...SUBMIT...
I am thinking I might need to store the submitted values in an array, then replace the value="<?= $data[id] ?>" with value="checked" but seems kinda wonky to me..
Anyone run into this before have an suggestions?
I solved this. I did not realize this, but there is an input value titled: checked which works like: checked="checked" .. Geeze this put me in my place : )

cannot update checkbox in php

Why can't the data in mysql not updated when I use checkbox, I already tried removing the input type hidden, but still didn't work. I think the problem must be in this part, because when I try to add records and check all of the checkboxes.
Then I will try to update it. By removing the checks in the other entry. The data is updated. the checks that are removed are also reflected in the database, but when I try to add checks on them again using the update module, it will not be updated. Please help, I'm just a beginner.
<td><input name="stats3" type="checkbox" id="sh"
value="<?php echo $row["STAT3"]; ?>" <?php echo $row["STAT3"] ? 'checked="checked"' : ''; ?> >Stockholder</td>
When you have a checkbox and you tick it, you get a key/value pair returned in the post to the server.
When the checkbox is not checked, it actually doesn't come back in the post (you can check this using Firebug, or by print_r($_POST)
This could be causing your problem.
You can either use:
if(isset($_POST['stats3'])) {
// checked is true
}
Or another easy solution is to have a select list with yes and no as options, which will always give you a value back - otherwise you need to set and unset the value based on the presence of the tick box in the post.
or you could check
if (isset($_POST['stats3']))
// value received
else
// value not received
<input name="stats3" type="checkbox" id="sh"
value="
<?php echo $row["STAT3"]; ?>
"
<?php echo $row["STAT3"] ? 'checked="checked"' : ''; ?>
>Stockholder
Its just working fine for me,
are if you are posting multiple values in the same name as "stats3" then you will have a problem

Categories