Jump to content

Executing a non checked box


Go to solution Solved by Ch0cu3r,

Recommended Posts

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 =<<<EOF
INSERT INTO "Urls" ("Domain", "Url", "Anchor", "User","AdminDisplay","UserDisplay")
VALUES ('$check', '', '', '$a','Y','');
EOF;

 }

Link to comment
https://forums.phpfreaks.com/topic/282850-executing-a-non-checked-box/
Share on other sites

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.

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 by Barand

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 =<<<EOF
INSERT INTO "Urls" ("Domain", "Url", "Anchor", "User","AdminDisplay","UserDisplay")
VALUES ('$check', '', '', '$a','Y','');
EOF;

 }

replaced with

$sql =<<<EOF
INSERT 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.

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.

  • Solution

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 by Ch0cu3r

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.