0

My checkbox looks like this:

<input type="checkbox" name="activate[]" class="setSetting" value="<?php echo $row["id"]; ?>"> 

And then i have a foreach:

$activate = $_POST['activate']; foreach($activate as $a){ echo $a ."<br>"; } 

Works fine to get the value out of. But how can i determine if the checkbox has been checked?

$activate = $_POST['activate']; foreach($activate as $a){ $query_email = mysql_query("SELECT id FROM lp_email_settings ORDER BY id ASC"); while($ro = mysql_fetch_row($query_email)){ $getUserSettings = mysql_query("SELECT * FROM users_email_settings WHERE uID = '$USER' AND eSetting = '$ro[0]'"); if($ro[0] == $a){ if(mysql_num_rows($getUserSettings) != 1){ mysql_query("INSERT INTO users_email_settings (uID, eSetting) VALUES ($USER, $ro[0])"); } }else{ mysql_query("DELETE FROM users_email_settings WHERE uID = '$USER' AND eSetting = '$ro[0]'"); } } echo $a."<br>"; } 

4 Answers 4

3

Only those checkboxes will be submitted that are considered successful (i.e. checked). That means only the checked checkboxes are available in $_POST['activate'].

And to determine whether a checkbox has been checked, you can use array_search to check whether a particular ID value is in $_POST['activate']:

array_search('12345', $_POST['activate'], true) 

And if you change your control’s name to use the ID as key like this:

<input type="checkbox" name="activate[<?php echo $row["id"]; ?>]" class="setSetting" value="<?php echo $row["id"]; ?>"> 

Then you can simply use isset or array_key_exists on $_POST['activate']:

isset($_POST['activate']['12345']) array_key_exists('12345', $_POST['activate']) 

Edit    As already said in the comments, you should rather iterate the available options and check for each option whether it’s already active and needs to be activated or deactivated. You can do this as follows:

$activate = array_flip($_POST['activate']); $query = "SELECT t1.id, t2.eSetting FROM lp_email_settings t1 LEFT OUTER JOIN users_email_settings t2 ON (t1.id = t2.eSetting) ORDER BY t1.id ASC"; $result = mysql_query($query); $insert = array(); $delete = array(); while ($row = mysql_fetch_row($result)) { if ($row[1] === null) { // option is not set yet if (isset($activate[$row[0]])) { // option needs to be set $insert[] = $row[0]; } } else { // option is already set if (!isset($activate[$row[0]])) { // option needs to be unset $delete[] = $row[0]; } } } if (!empty($insert)) { $query = "INSERT INTO users_email_settings (uID, eSetting) VALUES ($USER, " . implode("), ($USER, ", $insert) . ")"; mysql_query($query); } if (!empty($delete)) { $query = "DELETE FROM users_email_settings WHERE uID = $USER AND eSetting IN (" . implode(", ", $delete) . ")"; mysql_query($query); } 

The first query will select a left join of all available options and the already active options. So the result set is the ID of the option in the first column and the second column is either NULL if the options is not active or otherwise again the ID. So if there are three options (e.g. 1, 2, and 3) and only the options 1 and 3 are already set, the result should look like this:

 id | eSetting ----+---------- 1 | 1 2 | NULL 3 | 3 

So if $row[1] is null (inactive option), the ID in $row[0] is added to the $insert array if the corresponding option was set in the request ($activate[$row[0]], the keys/values are flipped to have a direct access). The same is done for those options that are already active but were not set in the request.

At the end the collected options in $insert are inserted and those in $delete are removed.

Sign up to request clarification or add additional context in comments.

5 Comments

Hmm... in my foreach, I am going to update setting column in users_email_settings, but i can only update a setting to "1", because if i dont check a setting(dont want it) i cant determine that i havnt checked it, therefore i have no place to make a query that sets setting to "0". What do I do in this case? My checkboxes comes from an while loop (if it could be to any help).
@Karem: You need a list of all available options. Then you can compare that list to the list provided by the POST request and update the settings accordingly. So you could iterate the list of all available options and check for each option whether it is in the list of options sent in the request. If so, set the associated setting to 1, and set it to 0 otherwise.
@gumbo ok, i made it now, please see updated question. Although it only inserts 1 row with the last id? What have i done wrong?
Thanks for the explanation. I tried your script, and it inserts just perfectly. But it doesn't delete/set to null those i havnt checked?
@Gumbo just found out where it went wrong: at the query in $delete, you made implode ( $insert ) it should be the $delete array there. Did that and now it works just fine
1

If you get a value it has been checked, otherwise you get nothing...

Here is a pretty good tutorial explaining how it works: http://www.html-form-guide.com/php-form/php-form-checkbox.html

Comments

1

Checkboxes will only be submitted at all if they are ticked. If they are unticked, PHP won't see them in $_POST.

I assume since you're using square brackets (ie activate[]) that you have more than one of them all with the same name. This will make it very hard to tell which ones were ticked, since you'll just get an array of the ones which were ticked; you'll know how many, but not which ones.

To get around this, you either need to give them all different names, or specify the array key for each one in the HTML code - ie name="activate[4]" or name="activate[bob]". Depending on the context, this could be an ID of the data you're activating, or the name of the feature, or anything else you decide, as long as it's unique for each field.

You will then still get the array only containing the ones which were ticked, but you will be able to tell which ones they were in your foreach loop by looking at the array key.

Hope that helps.

Comments

-1

You can check against each submitted value ($activate) to see if it does actually contain anything/fits your criteria:

 $activate = $_POST['activate']; foreach($activate as $a){ if($activate){ echo $a ."<br>"; } } 

Though the array should only contain those values checked.

1 Comment

You pretty much echoed the original code without explaining what happens as the other answers do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.