PHP: Determine whether its a checkbox is checked or not -
my checkbox looks this:
<input type="checkbox" name="activate[]" class="setsetting" value="<?php echo $row["id"]; ?>">
and have foreach:
$activate = $_post['activate']; foreach($activate $a){ echo $a ."<br>"; }
works fine value out of. how can determine if checkbox has been checked?
$activate = $_post['activate']; foreach($activate $a){ $query_email = mysql_query("select id lp_email_settings order id asc"); while($ro = mysql_fetch_row($query_email)){ $getusersettings = mysql_query("select * users_email_settings uid = '$user' , esetting = '$ro[0]'"); if($ro[0] == $a){ if(mysql_num_rows($getusersettings) != 1){ mysql_query("insert users_email_settings (uid, esetting) values ($user, $ro[0])"); } }else{ mysql_query("delete users_email_settings uid = '$user' , esetting = '$ro[0]'"); } } echo $a."<br>"; }
only checkboxes submitted considered successful (i.e. checked). means checked checkboxes available in $_post['activate']
.
and determine whether checkbox has been checked, can use array_search
check whether particular id value in $_post['activate']
:
array_search('12345', $_post['activate'], true)
and if change control’s name use id key this:
<input type="checkbox" name="activate[<?php echo $row["id"]; ?>]" class="setsetting" value="<?php echo $row["id"]; ?>">
then can use isset
or array_key_exists
on $_post['activate']
:
isset($_post['activate']['12345']) array_key_exists('12345', $_post['activate'])
edit as said in comments, should rather iterate available options , check each option whether it’s active , needs activated or deactivated. can follows:
$activate = array_flip($_post['activate']); $query = "select t1.id, t2.esetting lp_email_settings t1 left outer join users_email_settings t2 on (t1.id = t2.esetting) order t1.id asc"; $result = mysql_query($query); $insert = array(); $delete = array(); while ($row = mysql_fetch_row($result)) { if ($row[1] === null) { // option not set yet if (isset($activate[$row[0]])) { // option needs set $insert[] = $row[0]; } } else { // option set if (!isset($activate[$row[0]])) { // option needs unset $delete[] = $row[0]; } } } if (!empty($insert)) { $query = "insert users_email_settings (uid, esetting) values ($user, " . implode("), ($user, ", $insert) . ")"; mysql_query($query); } if (!empty($delete)) { $query = "delete users_email_settings uid = $user , esetting in (" . implode(", ", $delete) . ")"; mysql_query($query); }
the first query select left join of available options , active options. result set id of option in first column , second column either null if options not active or otherwise again id. if there 3 options (e.g. 1, 2, , 3) , options 1 , 3 set, result should this:
id | esetting ----+---------- 1 | 1 2 | null 3 | 3
so if $row[1]
null (inactive option), id in $row[0]
added $insert
array if corresponding option set in request ($activate[$row[0]]
, keys/values flipped have direct access). same done options active not set in request.
at end collected options in $insert
inserted , in $delete
removed.
Comments
Post a Comment