Posts: 7
Threads: 3
Joined: Nov 2011
Reputation:
0
Hello Ben/Seniors
Hope everyone is well!
I have a checkbox value doesnt get updated when user goes back to change. If the checkbox gets checked once, the resulted value forever remains as checked regardless what update follows thereafter.
Is this the very nature of a checkbox or something i did wrong?
Scenarios OK:
FORM page (by default: unchecked) --> REVIEW page (unchecked)
FORM page (checked) --> REVIEW page (checked)
Scenarios NOT OK:
FORM (checked) --> REVIEW (checked) --> goes back to FORM (update: unchecked) --> REVIEW (still checked)
My code:
<input type="checkbox" id="only genius knows php" name="only genius knows php" value="Yes" onclick="disable_dropdown(this.checked)" <?php if (@$fields["only genius knows php"] == "Yes") echo "checked"; ?> />
Big thanks in advance to any help!
Posts: 2,456
Threads: 39
Joined: Dec 2008
Reputation:
6
Dec 4th, 2011, 2:08 PM
(This post was last modified: Dec 4th, 2011, 2:09 PM by Ben.)
Ahh! Yeah, this just tripped me up myself.
So the funny thing about checkboxes is that if nothing's checked, nothing will be sent along in the POST request and that particular field won't be updated in the database. Sadly, this is just the way HTTP works for form submissions.
So to get around it with API forms, you need to add in a line of code to say if the form has been submitted and there's nothing for the checkbox field, set it to blank. Otherwise, store whatever value is being passed.
Here's some example code:
PHP Code: <?php
$fields = ft_api_init_form_page(); // ...
if (isset($_POST["your_submit_btn_name"])) { $_POST["cb_name"] = (isset($_POST["cb_name"])) ? isset($_POST["cb_name"]) : ""; }
// ... $params = array( // ... ); ?>
You'll need to change "your_submit_btn_name" to, well, your submit button name, and "cb_name" to whatever name attribute your checkbox fields have.
I put the other code in just to give you an idea of where it should go.
Hope this helps!
- Ben
Posts: 7
Threads: 3
Joined: Nov 2011
Reputation:
0
(Dec 4th, 2011, 2:08 PM)Ben Wrote: Ahh! Yeah, this just tripped me up myself.
So the funny thing about checkboxes is that if nothing's checked, nothing will be sent along in the POST request and that particular field won't be updated in the database. Sadly, this is just the way HTTP works for form submissions.
So to get around it with API forms, you need to add in a line of code to say if the form has been submitted and there's nothing for the checkbox field, set it to blank. Otherwise, store whatever value is being passed.
Here's some example code:
PHP Code: <?php
$fields = ft_api_init_form_page(); // ...
if (isset($_POST["your_submit_btn_name"])) { $_POST["cb_name"] = (isset($_POST["cb_name"])) ? isset($_POST["cb_name"]) : ""; }
// ... $params = array( // ... ); ?>
You'll need to change "your_submit_btn_name" to, well, your submit button name, and "cb_name" to whatever name attribute your checkbox fields have.
I put the other code in just to give you an idea of where it should go.
Hope this helps!
- Ben
Awesome! Fabulous! Gorgeous! Brilliant!
That works beautifully and it has now solved my problem.
Thanks Ben, Genius!
Posts: 2,456
Threads: 39
Joined: Dec 2008
Reputation:
6
Posts: 4
Threads: 1
Joined: Apr 2012
Reputation:
0
I am having the same issue with an array of checkboxes.
What would be the code for this?
Thanks,
Randy
Posts: 143
Threads: 19
Joined: Dec 2010
Reputation:
5
Oh my. I'm surprised I don't remember reading this thread before. Ben - please tell me that this was addressed in a core update, where you made this code standard? I haven't run into this, but I haven't been looking for it, either.
Not a FT employee, just a FT user lending a hand. If I've been helpful, please rate.
Posts: 4
Threads: 1
Joined: Apr 2012
Reputation:
0
I did a work around on this by adding another element to the array that is hidden in the form.
Posts: 143
Threads: 19
Joined: Dec 2010
Reputation:
5
Could you be more specific?
Not a FT employee, just a FT user lending a hand. If I've been helpful, please rate.
Posts: 143
Threads: 19
Joined: Dec 2010
Reputation:
5
Sep 12th, 2013, 12:50 PM
(This post was last modified: Sep 12th, 2013, 12:58 PM by michatmaster7.)
I admit I'm just getting around to this. I thought it would be a great addition to all my forms, because rather than have the form send a blank value, I could tell the form "if the checkbox is left unchecked, the send {some value}, otherwise send it's checked value."
Well. It half worked.
Now when the form is submitted, if the checkbox is left unchecked, it records the value from Ben's code above. However, if it IS checked, it always records a value of "1" - regardless of what the actual value is. This, of course, is not ideal.
I have most checkboxes set as text fields in the FT backend, but that shouldn't matter. So... is there a problem with the code above? Why is submitting a "1" when the box IS checked?
Example Code:
PHP Code: // ...
if (isset($_POST['submit_button_name'])) { $_POST["cb_name"] = (isset($_POST["cb_name"])) ? isset($_POST["cb_name"]) : "None";
// ...
<input name="cb_name[]" type="checkbox" value="Value if selected." <?php if (@$fields["cb_name"] == "Value if selected.") echo "checked"; ?> />
Not a FT employee, just a FT user lending a hand. If I've been helpful, please rate.
Posts: 9
Threads: 2
Joined: Jan 2011
Reputation:
0
Figured I would post the solution to the issue with "1" being echoed.
Slight change:
if (isset($_POST["your_submit_btn_name"])) {
$_POST["cb_name"] = (isset($_POST["cb_name"])) ? "WhateverYouWantToEcho" : "";
}
The check logic goes, "when the user hits the submit button, test if the field (checkbox) is checked, if yes, echo this, if not, echo that."
|