Sending unchecked checkboxes to PHP - php

I have a checkbox field in my HTML table. The table is generated dynamically, and the field is stored in an array as follows:
<input type="checkbox" name="checked[]" value="1">
In the PHP, I am inserting the checked rows into the table, but I also need to display the rows that have not been checked to the user after the submission, but there is no way of knowing which rows were not passed since checked[] for unchecked checkboxes are not being submitted.
What I want to achieve is the user is displayed a table with multiple rows, he checks which rows he wants to add to the database. After form submission, a page is to display which rows were inserted and which rows were not selected by the user. The unchecked rows need not be inserted into any database, but should be displayed to the user only ONCE, right after the submission, so that he can print the page for record purpose.
What is the best way to tackle this problem?

since you are creating the chekbox in first place so you know the total number of checkbox.
Total Checkbox minus Checked checkbox will give you what you want i.e unchecked box.
unchecked boxes = total checkboxes - checked checkboxes ;

Use 3 arrays, 1 holding the options you are sending, one with the responses from the user and one empty that will hold the difference. The difference between the two will get you the unchecked fields.
<?php
$myOptions = array('option1', 'option2', 'option3');
$userArray = array('option2'); // This is your $_POST['checkboxes'] array
$leftOptions = array();
foreach ($myOptions as $value){
if (!in_array($value, $userArray)){
$leftOptions[] = $value;
}
}
?>
Note: you can replace the foreach with array_diff as #deceze mentioned in the comment.

Related

Getting values of all checked checkboxes in PHP and put them in an array

I'm creating a CMS in which I have an overview of pages. I want the user to be able to mass delete these pages and so I have created a form in which each page has a checkbox with the pages database ID as value and name:
<input class="mass-delete-check" type="checkbox" name="<?=$page["id"]?>" value="<?=$page["id"]?>" id="<?=$page["id"]?>">
Now when I submit this form I need to get the values of the checkboxes that are actually checked and put them in an array I can go through to delete them. The thing here is that I will have to get checkbox values based on if they are checked and not on their name because I can't know all names.
Does anyone have a solution to this?
Use the same name for all checkboxes. So after submiting you will have array with page IDs to delete.
<input class="mass-delete-check" type="checkbox" name="delete_pages[]" value="<?=$page["id"]?>" id="<?=$page["id"]?>">
After submit you would get array of IDs with $_POST['delete_pages'], which contains actual page IDs what you need to delete.

Insert "Selected Users" into another table

Okay I am having all kinds of problems trying to figure the way to script this.
What I want to do is have a list of people displayed from a mysql database.
Once list is generated I would like to have a checkbox next to each person.
Now here is the tricky part (at least for me), for each person checked I would like to enter it into only 1 column in another table in the database. We will call it "rsvp" I for the life of me cannot figure this out.
Basically I would need to enter user id's from the checkbox ex these are id's checked "1,4,5,6,8". Now I would need to enter that into a database table "rsvp" and then be able to go back and modify that same entry with the checkboxes already selected.
I think I am way over thinking this and it is actually a very simple solution.
You should
1) Get the result set from the database
2) Get an array of selected members
3) Iterate of the actual dataset and keep generating check boxes, if id matches the selected id, generate the check box as checked.
4) On submitting the form, clear old entries and re-insert whatsoever has been selected.
<form>
<?php
$resultSet = getAllTheUsers here ////
$arrayOfSelectedUsers= getAlreadySelectedUsers();
// keep the array as "id"=>'User name'
while(result is not empty){
if(array_key_exists('bar', $foo))
echo "<checkbox value=$result['userId'] checked='checked' name='rsvp' />";
else
echo "<checkbox value=$result['userId'] name='rsvp' />";
}
?>
<input type='submit' />
</form>
This should give you an idea. Once the form is submitted, get all the values, delete the existing entries from the database and re-insert what so ever has been selected again.

Filtering results from AJAX checkboxes in PHP

I have a few checkboxes on the front end of my system whereby the user checks the boxes to filter results from the database results. There are 6 checkboxes.
In the database, each profile can have one or more of these checkbox values assigned to them, so by clicking a checkbox the user is filtering through many profiles and each one has to contain the checkboxes that are currently ticked.
My problem is that I have it half working, but cant quite seem to figure out the logic about how to finish it. There still seems to be values in there that dont match:
$pieces = explode(", ", $row['competency']);
foreach(array_keys($_POST['competencyFilter']) as $filter){ //each chosen
if(in_array($filter, $pieces)){ //if this chosen is in the db
if(in_array($row['ID'], $resultList)){
}else{
$resultList[] = $row['ID'];
}
}else{
if(($key = array_search($row['ID'], $resultList)) !== false) {
unset($resultList[$key]);
}
//continue 2;
}
}
Iam not sure if the logic here is too complicted for what I want, but at the moment it loops through all the values that are currently ticked in checkboxes, and for each one, if it exists in a profile, it adds the profile ID to an array.
The problem is that some profile ID's remain in the final array $resultList[] even when a tickbox is ticked that isnt in that profile.
I hope this makes sense...
EDIT
Here is the array data in each profile stored in the DB (obviously will contain different values for each profile, but there are 6 all together):
thermXMbb, thermXPbb, thermXMrf, thermXPrf, .. , ..
There are 6 all together, and there are also 6 checkboxes, one for each of these.

Get Row value from custom gridview when checkbox is checked

I am developing apps in Codeigniter, i am stuck in some code...i just want to know how can i get complete row values if any one check the checkbox and submit, so i should get row value of selected checkbox....
You can use array for while creating rows, use name for your each row's input field as array and when you submit the your form loop over your checkbox array and get the values from other elements using the same key which selected checkbox is having

Submit form and send data through AJAX at the same time?

Let's say I have a simple form, with a select combobox and a table that has a checkbox for each row, to choose the rows you want.
Now, on the server-side, I need to associate all the items selected from the table (using the checkbox for each row), with the item selected in the combobox.
I know the value in the combobox will be submitted with the form, but how can I send all the selected elements from the table? do I have to use AJAX or something? or is it possible to do it via POST/GET?
The form needs to enclose (be an anscestor of) both the table and the combo box.
The table row checkboxes should all look like
<input type="checkbox" name="rows[]" value="ROW ID"/>
And then on the server side you'll get, in addition to the combobox value, $_POST['rows'] as an array containing the ids of the checked rows.

Categories