I have this database where I have to capture a lot of yes/no questions, and the prefered method for the users is checkboxes. Everything is working as it should, except when it comes to retreive and show the values. Unchecked boxes return values of "0"
Is there anyway to either ignore and not display "0" in the reports OR change the default value from "0" to blank"
result = $this->input->post('checkbox',TRUE)==null ? 0 : 1
this is nothing to do with CodeIgniter. :)
How about this?
if($_POST['checkbox']==0)
$_POST['checkbox'] = '';
It is returning 0 because that is false and that's what check boxes returned when not checked. Just add an if in your CI processing method that returns whatever value you want if the checkbox value==0.
Edit: Just to clarify, this doesn't have anything to do with your CI. What I mean by it is returning 0 is that that is what the form itself is returning - that the behavior of a checkbox. To change the value will take a quick check in your CI code to change the 0 to a value you want. I assume you are accessing the value somewhere to create your email.
since the the checkbox returning null value while unchecked, you won't get a value while posting. Here is a simple quick solution for returning unchecked value from checkbox,
<input type="hidden" name="cbox" value="0" />
<input type="checkbox" name="cbox" value="1" />
if( ! $_POST['checkbox']) $_POST['checkbox'] = '';
If a checkbox is not checked, no data is sent for it. (If it is checked, the value attribute is sent). Attempting to read $_POST['checkbox'] will cause a PHP error (unless you use empty() or isset(), which are special language constructs).
this->input->post('checkbox') will return FALSE if the checkbox data is not set, i.e. if it was not checked. There's no way to change that value, I'm afraid. If you want a different value, you will need to manually compare FALSE and use a different value.
Finally, when you submit FALSE to the database using CodeIgniter's DB access API, it is converted to '0'. This would work well for a boolean column in the database. When you read out a boolean column, you'll get whatever your database's preferred code for TRUE is, regardless of what you submitted originally. (With the possible exception of sqlite, which is weakly-typed, a bit like PHP is).
Related
Here's a simple example:
echo 3==3; // echoes 1
echo 3==2; // should echo 0, yet echoes nothing
I noticed this as I was designing a form which entails a checkbox. When ticked, the checkbox passes a value of 1, when unticked, rather than passing a value of 0 as one would expect, it passes nothing; an undefined boolean so to speak.
I tried solving this with the following code:
$myBool = isset($_POST['myCheckbox']);
However, this doesn't seem to work either.
One solution to passing the checkbox value to $myBool would be to do some if-else conditioning, but I want a more concise solution. Is this possible?
In PHP, every decision makes statement returns a boolean value, either true or false. So, echo 3==3; is a true statement, That's why it returns 1 and echo 3==2; is a false statement, that's why it returns nothing.
But if you want to echo out the associated integer of false (which is 0), then you can use typecasting. Like:
echo (int)(3 == 2);
It will print out 0.
Test Code
// http://php.net/manual/en/language.operators.comparison.php
echo 3==3;
echo "<br>---------------<br>";
echo 3==2;
echo "<br>---------------<br>";
var_dump(3==3);
echo "<br>---------------<br>";
var_dump(3==2);
Output
1
---------------
---------------
boolean true
---------------
boolean false
The reason is for no output is you are trying to echo the boolean "false", which prints nothing.
The statement 3 == 2 evaluates to false, and as noted in the PHP documentation for echo under the examples section, running echo false won't print anything because it's not technically a function. However, you can use var_dump instead.
echo 3 == 2; // no output
echo false; // no output
var_dump(3 == 2); // outputs bool(false)
var_dump(false); // outputs bool(false)
The actual reason for your question about why a statement that evaluates to echo false; prints an empty string, is given in the documentation on strings, in the section about casting to string:
Converting to string
A value can be converted to a string using the (string) cast or the
strval() function. String conversion is automatically done in the
scope of an expression where a string is needed. This happens when
using the echo or print functions, or when a variable is compared to a
string. The sections on Types and Type Juggling will make the
following clearer. See also the settype() function.
A boolean TRUE value is converted to the string "1". Boolean FALSE
is converted to "" (the empty string). This allows conversion back and
forth between boolean and string values.
The reason that you can't test an unchecked checkbox, the way you tried, is that browsers don't send unchecked checkbox states on form submissions.
Typical solutions to this conundrum are:
Keep track of all available checkboxes in the backend and compare them upon receiving a form submission.
Add a hidden field in your HTML form before the actual checkbox, giving it the same name as the checkbox and a value that represents the unchecked state.
<input type="hidden" name="checkbox1" value="0">
<input type="checkbox" name="checkbox1" value="1">
These hidden fields will be submitted upon form submission and will be overwritten by the checkbox value if it is checked1.
1) The values will actually both be send if the checkbox is checked, but PHP will overwrite any earlier received value with a later received value if they have the same parameter name (unless the parameter name represents an array without explicit key names, like name="checkbox[]" instead of name="checkbox[1]").
I'm having some issues with codeigniter's validation filter when using checkboxes. I typically used the numeric filter for checkboxes, presuming it would filter for 0 or 1 but now I see there are several instances in which this fails.
Does anyone know a regex that I can put in the preg_match to validate a checkbox?
I would like this to allow for booleans and a few others 1, 0, null, true, false, empty etc...
A checkbox only returns one value. It's value (as indicated in its value= attribute) or 'true'. If the checkbox is not selected, it is not passed in the POST request. Therefore, for validation, you only need to check for 2 things:
Was it even selected in the first place?
Is its value consistent with what you expect it to be?
So:
if (isset($_POST['checkbox']) && ($_POST['checkbox'] == 'true') { //or whatever value you want
Should do the trick nicely. Unless I've misunderstood your question in which case please comment.
Checkboxes either have a value or they don't. If they have a value they're checked, if they don't then they're not checked.
A regex would be serious overkill here, you only need to check if the checkbox exists in the submitted data.
$checkBoxChecked = isset ($_POST['checkbox_name_goes_here']);
I have a bunch of values from checkboxes that are boolean. Al I want to do is set them to yes if they are 1 and no if they are 0.
My code fails, looks ok to me?
$item = $form_state['values']['item1'] == 1 ? 'Yes' : 'No';
If your checkboxes have value attribute equal to 1, it should be OK:
<input ... type="checkbox" value="1" />
If you have not set these values or in all the cases you may just check, if they exists in $_GET or $_POST array (assuming $form_state is taken from there):
$item = isset($form_state['values']['item1']) ? 'Yes' : 'No';
The above example, should works for you. Keep in mind radio buttons and check boxes will not be set in $_GET or $_POST if they are not selected, at all, which also may generate Notice or Warning, if trying to access non-existing index.
In older versions of php you might need to use $_REQUEST.
I was wondering if this is possible.
The serialize-string will be inserted in a mySQL-database, so it would be fine if all checkboxes would be inserted and not only the ones that is ticked(on).
it's possible to make on column in the database for each of the checkboxes, and use this if/else-statement:
if(isset($_GET['checkbox'])) {
// It's checked!
}
else {
// not checked!
}
but its a bit unappropriate...
Give all the checkboxes the same name (ending in [] since this is PHP)
Give all the checkboxes different values
Have an array of all possible values in the script (you can use the same array when generating the HTML for the form!)
Loop over it and use in_array to determine if you should set it to true or false
Pass the result to your database function
The form that will be submitting the data will have somewhere between 10 and 100 values being sent to the PHP file. The number of inputs is stored in a variable called count within my javascript function but I don't know how to transfer its value to my PHP file.
Another idea I had was to have a while loop that detected a null value and to have a counter within the while loop to keep count.
How should I handle this?
No need to submit the count. You can just submit your dynamic number of values as an array. You can use the PHP array notation in your name attributes:
Input 1: <input type="text" name="myvar[]" />
Input 2: <input type="text" name="myvar[]" />
...
Input n: <input type="text" name="myvar[]" />
On the PHP side, $_REQUEST['myvar'] will be an array.
Without knowing more about your implementation, I would suggest the latter of your two options: use PHP to loop through your $_POST array and count the valid values you have. This is the best option as it is more secure and reliable.
I think there might be a more "correct" way to do this, but what I would do (without knowledge of a better way) is have a hidden form element something like
<input id="hidden_count" type="hidden" name="count" value="" />
And then have a function called onsubmit that sets this value and returns true to tell the form to continue
(with jQuery)
function onSubmitFunc(){
$('#hidden_count').val(count);
return true;
}
I'm sure there's a more elegant solution, but that should work for what you need.
If you have access to Javascript, the easiest by far would be to turn your data into JSON on the client side, send it to the server as a single string variable, and use json_decode on the server to get the properly formatted value back. You can react to the submit event on your form to have the Javascript compute the JSON before the data is sent.
Otherwise, you can have the Javascript output the number of fields to a specific hidden variable within your form. The server can then read that value, and look for the data by key in its input.
I guess you use javascript for creating the additional input element, if you just name ascending, you could use count to loop through the $_POST data. Or else use a hidden element form for counting the new input elements and post it, that way you have the correct number, and know how long you should be count.
Maybe I am not getting this, but if count in your JS file corresponds to the number of input elements in the form which is send to your PHP file, why not count() them on the serverside. The data will be in the $_POST array.