The form used to add a new item into the database and edit existing items is the same form. A "Mode" is passed into the form to tell it if were adding something new or to load the existing item for editing. So....
<input type="checkbox" name="fflreq" id="fflreq" value="<?=$row['FFLr']?>" <?php if ($row['FFLr']=="Yes") {echo 'checked';} ?>>
When a new item is being added, $row['FFLr'] doesn't exist so of course the value is BLANK or NULL or i guess 0 if i don't initially check the checkbox- The form processor coverts this into a "No" and inserts it into the database.
Now here is my problem - When I come back to a item and the form is in edit mode, the VALUE in this checkbox is now "No" - when I am clicking the checkbox to change its status, I see the checkbox become 'checked' but the value is not changing. in other words the click/check status is not setting the value of $_POST['fflreq'] to YES or 1.
I thought, that checking or unchecking a form checkbox replaces whatever is currently in the value='' attribute with a 1 or 0 to represent yes/no on/off or whatever. Why would the value pulled in from the database not change on form submission?
You need to do it in this way:
<input type="checkbox" name="fflreq" id="fflreq" value="Yes" <?php if ($row['FFLr']=="Yes") {echo 'checked';} ?>>
and when submit the form if the above checkbox is checked then you recieved the $_POST["fflreq"] in the form submit page and if it is not checked you recieve nothing in $_POST
so in the submit page you can do this:
$fflreq = "No"
if(isset($_POST["fflreq"]) && $_POST["fflreq"] == "Yes")
{
$fflreq = $_POST["fflreq"];
}
//then you can simply do anything with the $fflreq such as inserting it into database etc.
I hope this can be of some help.
That's not how it works. If you have "checked" the check box then it (along with it's value) will be sent with the post/get (i.e. submission) of the form. If you haven't checked it, then it won't be set...
If the checkbox is active, the browser sends the key/value pair defined in the input tag. However, if the checkbox is not active, nothing at all is sent for this checkbox.
There are two options to deal with this:
The clean option is to be aware of this on the server side, and assume that the checkbox was not active whenever no value comes through.
A more dirty variant is having a <input type="hidden"> tag just before the checkbox, using the same name, but the value you need to see when the checkbox is inactive. This way, when the checkbox is active, you'll still get the desired value from the checkbox, because it will overwrite the hidden value. However, if the checkbox is inactive, you'll get the value from the hidden field.
Not really, the check/unchecked status is read out by looking if the HTML name attribute value is present in the $_POST param.
You can check this with:
<?
if (!empty($_POST['fflreq'])){ /*checked*/ }
else{ /*unchecked*/ }
?>
The value of the HTML attribute value always stays whatever it is in your HTML. So no user interaction (except JS) can change that.
Working with PHP empty() function lets you bypass all the "Yes" "1" string int casting issues.
Further I would use ternary notation for these kind of things:
<input type="checkbox" name="fflreq" id="fflreq"
value="<?=$row['FFLr']?>" <?=(!empty($row['FFLr'])?'checked':'')?>>
Related
I'm building a registration form in Concrete5 but beforehand I need to retreive the User Attributes.
I'm using hidden input to get the values as so:
<input type="hidden" name="akID[<?php echo UserAttributeKey::getByHandle('school')->getAttributeKeyID(); ?>][value]" value='<?php echo $_POST['full_name']; ?>'/>
However I can only get the value if I make a mistake beforehand, i.e. incorrect confirm password and then resubmit the form.
Any ideas on how to get the values without doing this?
Kind Regards
Your example is a bit confusing.
First let me explain why it doesn't work the way you want it to.
You are giving your hidden field a value from $_POST. $_POST only exists once you actually post the form. So on first load of the page, $_POST doesn't exist so $_POST['full_name'] is null.
When you submit the form and there is an error however, the same page reloads but this time $_POST exists since the page reloads after submitting the form.
Here's what is confusing. If on reload $_POST['full_name'] has a value it means you already have a 'full_name' field probably as a text input box. Why then do you want to have a hidden field with the exact same value?
If what you want in this hidden field is the value of a user attribute you need to do 2 things:
1- make sure the user is logged in, else no attributes are available
2- get the user info object to get the attribute value from like so:
$u = new User();
$ui = UserInfo::getByID($u->getUserID());
2- modify the value of the hidden field like so:
value="<?php echo $ui->getAttribute('attribute_handle'); ?>"
So lets say I have a checkbox
<input style="margin-left:0px;" type="checkbox" value="1" name="disablePause" class="product-options-cbx" <?= $this->oProduct->getDisablePause() == 1 ? 'checked="checked"' : ''; ?> /> Disable Pause
This checkbox will pull a value of 1 or 0 from the database and if it is 1 will load checked. what happens is when the user deselects the checkbox then submits the form instead of passing 0 to the db it is erroring. I'm pretty sure my error lies within the value tag. any suggestions? Ultimately, I just want to be able to pass 1 or 0 back and forth depending on if box is checked (1) unchecked (0)
Thank You!
If the value of a checkbox is 0, it doesn't actually send that param to the "receiving" page. If you are using a simple 1 or 0 value then you can just listen for it on your parsing page by using isset()
$disablePause = isset($_REQUEST['disablePause']) ? 1 : 0;
The problem is that an unset check box sends no data, not a 0. (That is, an unset checkbox behaves identically to a checkbox that does not exist). You need to use PHP's isset() function to test if the checkbox is set:
<?php
if(isset($_POST['disablePause']))
{
//the checkbox is set
} else {
//the checkbox is not set
}
?>
You do this wherever you check whether the checkbox is set.
I have a few checkboxes in my form and I need to change their value when the checkbox is checked.
For example this is one of the checkboxes:
<input type="checkbox" name="drink" value=""/>
I know .val() can change it but I wasn't able to do this with if statement.
I appreciate your answer in advance.
What you are seeking is actually very meaningless. See, when a form is submitted, only the checked checkboxes actually send values, so it makes no sense to change the value especially for the unchecked checkbox.
Better Solution
You should instead give it the "checked" value, and keep it that way, that will cause it to submit correctly even without changing the values.
You can add a click event listener on the checkbox:
$('input[name="drink"]').click(function() { // when click on it
if ($(this).attr('checked')) { // if the checkbox is checked
$(this).val("value #1"); // change the value
} else { // otherwise if is unchecked
$(this).val("value #2"); // change the value
}
});
// attach an onchange handler to all checkboxes on the page
$("input[type='checkbox']").change(function() {
// or if($(this).prop("checked")) {
if(this.checked) {
$(this).val("value for checked");
} else {
$(this).val("value for unchecked");
}
});
The onchange event is the one you should be interested when it comes to checkboxes, as it fires whenever you change the checked state of it.
The checked property determines whether or not the checkbox has been checked.
The .val() method allows you to conveniently read/write element values.
I have a form with a checkbox that's used to filter some search results.
The form action is GET so that the users can navigate back to the search results without having to OK the post data request message.
I want one of the checkboxes to default to true. This in itself is not a problem of course, it's simple HTML. However, it's the PHP that powers it that I'm struggling to figure out.
When the user visits the page for the first time, there won't be any GET variables set, meaning thisCheckbox would be unset and all the relevant checks would evaluate to false. Meaning I can't do:
#this returns a false negative
if (isset($_GET['thisCheckbox'])) echo 'checked="checked"';
If the user explicity ticks the checkbox and submits, then it's fine, because $_GET['thisCheckbox'] will be true.
Is there a way of getting round this (without using radio buttons)?
You want to check if it is set, and if it is not, check for other variables that are indicative of a request having taken place.
Not very nice, but what about introducing a hidden field $_GET['filterApplied']. If the user submits the form, this field is set. Than you can do it like
if (!isset($_GET['filterApplied'] || isset($_GET['thisCheckbox'])) echo 'checked="checked"';
If you use JQuery, do :
$(document).ready(function(){
$("#thisCheckbox").attr("checked",true);
});
This should solve your problem.
If you don't use $_GET for anything else, you can also do
if( empty( $_GET ) || isset( $_GET['thisCheckbox'] ) ) {
echo 'checked="checked"';
}
This is an ugly approach, but it works. Add a hidden input field with the same name above your checkbox.
<input type="hidden" name="thisCheckbox" value="0">
<input type="checkbox" name="thisCheckbox" value="1">
Given that when using an html radio box, to send values to a php script, only the selected values is sent to the php script, still holds true.
Do I still need to check that the user has selected something, when I am only interested in the selected value being sent to the php script, if the user does not select anything we do nothing or I guess I could prompt the user to select something.
If so, whats the best way to handle this with regards to radio boxes?
<!--Using the radio box, a single selected value is sent to the php script for processing-->
<td width="100px" height="30px">
<input type = "radio"
name = "selection"
value = "Lays" />Lays Chips 0.99c<br />
<input type = "radio"
name = "selection"
value = "Ruffles" />Ruffles $1.85<br />
The user will be able to click the "submit" button even without selecting an option from your radiobox.
If it's ok to send the form without a selection and you just want to do something different (or ignore it) when nothing is selected, you can do this:
<?php
if (isset(($_POST['selection'])) {
//Use it.. and make sure to validate user input.
} else {
//nothing was selected. Do something or just ignore?
}
?>
OTOH, if you want to prevent submiting without a selection, you will need to use some JavaScript to do it.
Even if the radiobox is not set, you can still post the form back to the server. Unless, of course, you have Javascript that prevents one from clicking on the submit form if the radiobox is not set.
So, you still need to check whether the radiobox is set or not, before working on it.