saynotojava Posted October 9, 2013 Share Posted October 9, 2013 I have a code which do following:select data from database,and when you click on it,it will insert only those checkboxes which you selected. But the problems is,i want non checked checkboxes to be executed as well,they would execute same query except one difference-one field in structure should write n instead y then. Here is code: Selecting part: echo'<table><tr>'; while($row = $ret->fetchArray(SQLITE3_ASSOC) ){ echo '<td>' .$row['Domain'].'</td><td><input type="checkbox" name="a[]" value="'.$row['Domain'].'"></td>'; // echo '<td>A</td><td><input type="checkbox" name="a[]" value="n"></td></tr>';if ($i % 10 == 0) echo '</tr><tr>'; $i++;}echo'</tr></table><br/> ; Inserting Part: if(!empty($_POST['a'])) { foreach($_POST['a'] as $check) { $sql =<<<EOFINSERT INTO "Urls" ("Domain", "Url", "Anchor", "User","AdminDisplay","UserDisplay")VALUES ('$check', '', '', '$a','Y','');EOF; } Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/ Share on other sites More sharing options...
AbraCadaver Posted October 9, 2013 Share Posted October 9, 2013 Unchecked boxes don't get submitted. So you might want to assign the numeric indexes to the array and then check for isset() on each one. Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453329 Share on other sites More sharing options...
jazzman1 Posted October 9, 2013 Share Posted October 9, 2013 @Abra, what did you mean by saying - "Unchecked boxes don't get submitted". Everything which have a name and value inside a form could be submitted on the server. Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453331 Share on other sites More sharing options...
AbraCadaver Posted October 9, 2013 Share Posted October 9, 2013 @Abra, what did you mean by saying - "Unchecked boxes don't get submitted". Everything which have a name and value inside a form could be submitted on the server. No, unchecked checkboxes do not. Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453332 Share on other sites More sharing options...
jazzman1 Posted October 9, 2013 Share Posted October 9, 2013 Why? Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453333 Share on other sites More sharing options...
AbraCadaver Posted October 9, 2013 Share Posted October 9, 2013 They are not considered "successful controls" and so aren't submitted. Not sure why. I sometimes think it would be better to submit them and set them to empty if not checked and to the value if they are checked, but such is life. Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453335 Share on other sites More sharing options...
Barand Posted October 9, 2013 Share Posted October 9, 2013 (edited) Another method is use hidden fields with same names as the checkboxes defined before the checkboxes. Checked boxes submitted override the hidden values example <?php echo '<pre>',print_r($_POST['cbox'], true),'</pre>'; ?> <form method="post"> <input type="hidden" name="cbox[0]" value="0"> <input type="hidden" name="cbox[1]" value="0"> <input type="hidden" name="cbox[2]" value="0"> <input type="hidden" name="cbox[3]" value="0"> <input type="hidden" name="cbox[4]" value="0"> <input type="checkbox" name="cbox[0]" value="1"> 1<br> <input type="checkbox" name="cbox[1]" value="2"> 2<br> <input type="checkbox" name="cbox[2]" value="3"> 3<br> <input type="checkbox" name="cbox[3]" value="4"> 4<br> <input type="checkbox" name="cbox[4]" value="5"> 5<br> <input type="submit" name="btn" value="Submit"> </form> Edited October 9, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453336 Share on other sites More sharing options...
jazzman1 Posted October 9, 2013 Share Posted October 9, 2013 I was thinking that the server return boolean false if the checkbox is not checked but actually is omitted altogether. Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453340 Share on other sites More sharing options...
saynotojava Posted October 10, 2013 Author Share Posted October 10, 2013 OK i found partial solution:what i select i get properly inserted with Y,nonselected get inserted as well,but it doesn't show N on that field for some reason,it stay empty. Here is changes which i did: echo '<td>' .$row['Domain'].'</td><td><input type="checkbox" name="a[]" value="'.$row['Domain'].'"></td>'; replaced with echo '<td>' .$row['Domain'].'</td><td><input type="hidden" name="a[]" value="'.$row['Domain'].'"><input type="checkbox" name="b[]" value="Y"></td>'; foreach($_POST['a'] as $check) { replaced with foreach (array_keys($_POST['a']) as $key) and added after foreach : $check = $_POST['a'][$key]; $bleh = $_POST['b'][$key]; $sql =<<<EOFINSERT INTO "Urls" ("Domain", "Url", "Anchor", "User","AdminDisplay","UserDisplay")VALUES ('$check', '', '', '$a','Y','');EOF; } replaced with $sql =<<<EOFINSERT INTO "Urls" ("Domain", "Url", "Anchor", "User","AdminDisplay","UserDisplay")VALUES ('$check', '', '', '$a','$bleh','');EOF; Maybe i should just leave it as it despise there is no N written,after all it saves some space.But as after this i need to make update and delete part,maybe it will be required no matter what. Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453364 Share on other sites More sharing options...
saynotojava Posted October 10, 2013 Author Share Posted October 10, 2013 And now i figured out how to insert N with making an td line like this: echo '<td>' .$row['Domain'].'</td><td><input type="hidden" name="a[]" value="'.$row['Domain'].'"><input type="checkbox" name="b[]" value="Y"><input type="hidden" name="b[]" value="N"></td>'; But this causing strange bug-when i click on two values,first value is correctly added,but second Y doesn't go domain which i clicked,it goes to domain right after that one for some reason.And that repeats for every further selection,Y always write to domain after the one which i clicked. Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453393 Share on other sites More sharing options...
Solution Ch0cu3r Posted October 10, 2013 Solution Share Posted October 10, 2013 (edited) For each checkbox name and domain you also need to assign them the same unique key. <input type="hidden" name="a[unique_key_value_pair]" value="'.$row['Domain'].'"> <input type="checkbox" name="b[unique_key_value_pair]" value="domain"> <input type="checkbox" name="b[unique_key_value_pair]" value="domain"> If you don't do this then the checkbox values will not pair up with the hidden fields keys,and you'll get miss matched results. When you generate the checkboxes do echo '<tr><td>'.$row['Domain'].'</td> <td> <input type="hidden" name="a['.$i.']" value="'.$row['Domain'].'"> <input type="hidden" name="b['.$i.']" value="N"> <input type="checkbox" name="b['.$i.']" value="Y"> </td></tr>'; $i++; // increment unique id Before the loop that echo's the hidden fields/checkboxes add $i = 0; to instantiate the counter Now your foreach loop should work as expected Edited October 10, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453395 Share on other sites More sharing options...
saynotojava Posted October 10, 2013 Author Share Posted October 10, 2013 For each checkbox name and domain you also need to assign them the same unique key. If you don't do this then the checkbox values will not pair up with the hidden fields keys,and you'll get miss matched results. When you generate the checkboxes do echo '<tr><td>'.$row['Domain'].'</td> <td> <input type="hidden" name="a['.$i.']" value="'.$row['Domain'].'"> <input type="hidden" name="b['.$i.']" value="N"> <input type="checkbox" name="b['.$i.']" value="Y"> </td></tr>'; $i++; // increment unique id Before the loop that echo's the hidden fields/checkboxes add $i = 0; to instantiate the counter Now your foreach loop should work as expected Yep,it works. Quote Link to comment https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/#findComment-1453405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.