Serialize checkbox array, also get checkboxes with off-value - php

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

Related

passing jquery array to php - unexpected format and count() value and variable passing issue

I am passing a jquery array called 'selected' full of ids along with the opening of an ajax modal.
$('#dtDelete').on('click', function () {
$('#modal-ajax').load('/modals/m__delete.php?selected='+selected);
$('#modal-ajax').modal('show');
});
On the modal php page count($_GET['selected']); always returns 1 no matter what. I am trying to get an actual count of the number of values in the array. Turns out this is because the array is a string as noted below.
var_dump($_GET['selected']); returns something along the lines of string(69) "187419,187420,187413,187414,187415,187416,187417,187418,187421,187422" which is something I am not accustomed to (sort of new to jquery). I need to do processing in php using foreach on this array. Can I 'convert' this to a 'normal' php array so count() will work as expected and I can process it normally in php?
Lastly, this array may or may not be extremely large at times. The jquery function above opens an ajax modal (I am using the modal as a confirmation box for the user whether they really want to delete the entries in the selected array) and I know the $_GET method has limits to the amount of data it can pass. I can't do $_POST because this is a modal and I need to load it then show it... how else can I pass this data?
$_GET['selected']
returns the STRING after the attribute 'selected', and count(string) is 1 not matter what ( it's not a multi-dimension array to be greater than 1).
As for the comma separated string example you gave, you may use the following :
$selected = $_GET['selected'];
//test wether the string has contents
if(strlen($selected)!=0) {
$selected_array = explode(',',$selected); //this is the new array that you want
}
else {
//the string is empty
}
There are many string functions you may check at : http://www.php.net/manual/en/ref.strings.php

Check if same value exist in an array if not store into array

I can't figure out the logic to how to check if a value exist in an array and if it doesn't store it.
I have a array with information and I'm trying to just get the first instance since some of the values repeats, for example username it will show up more than once but I only need the first username I don't need to store multiple values of the same username in an array.
Righh now I'm using pushing all the valuse into the array as so
for ($i=0; $i<count($info); $i++) {
array_push($users, $info[$i]->username);
}
There are several ways to do this:
Loop through the array and check to see if the value exists. If not, add it.
Use in_array() to check to see if the value exists. If not, add it.
Add the value to the array and then use array_unique() to eliminate duplicate values.

Regex to validate checkbox input

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']);

jQuery serialize issue with php foreach

$("#doStatus").click(function()
{
var serialized = $("#formStatus").serialize()
$.get("<?echo $site['url'];?>modules/yobilab/tuitting_core/classes/doStatusBox.php", serialized);
window.setTimeout('location.reload()', 1000);
return false;
});
This code will return something like this:
tuitting=test&f%5B%5D=2&f%5B%5D=4&f%5B%5D=8&t%5B%5D=2&t%5B%5D=3&t%5B%5D=4
Where tuitting(the first value) is the value of the textarea, then we have:
-f this is the name of somecheckboxes that have the same names but different values
-t other checkboxes, same names, different values
As you may see the serialized function for "f" and "t" returns strange values, when they should only be simple number, like tuitting=test&f=1&f=3&t=9&t=10
Now when the ajax call the doStatusBox.php file nothing happens.
The doStatusBox.php file is made by a simple foreach php loop.
It will take the below variables:
$_GET['tuitting']
$_GET['t']
$_GET['f']
Foreach `$_GET['t'] and $_GET['f'] the loop will insert the relative values into the database.
This is not working at all. The php foreach can not recognize the results given by the jQuery serialize function and does not do simply anything.
What is the problem?
I should use another function instead of foreach?
Or the problem is in the jQuery serialized function?
Please help me.
`
Since we cannot see your HTML code, I can't say for sure what the problem is, but I suspect it's one of two problems:
(most likely) - your checkbox fields need to have a [] after the name. Example: name="t[]". If all of your checkboxes are name="t", then when multiple are posted, PHP will only see one of them. However, if you put brackets at the end of the name PHP will recognize the collection of values as an array that you can loop through.
(less likely) - the PHP script that receives this data should run urldecode() on the data. Example: $tuitting = urldecode($_GET['tuitting']);
Hope this helps!

Codeigniter unchecked checkbox value = blank?

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

Categories