0

How an i get unchecked Values from my ckeckbox?

I have 3 Radiobuttons. When i click the first all checkboxes must be checked, the second radiobutton unchecked all checkboxes. The third radiobutton gives the possibility to check some checkboxes, and all checkboxes who was not cheked i want to send the value to the DB from 0 to 1 with an UPDATE Query. The radiobuttons works fine, but i don't get the values of the unchecked checkboxes to send it to my update query. Here the code of the first page with the formelements:

    //That is only a short version of my Select who works correct
    $abfrageT = "SELECT DISTINCT id_dexpd, acores.code_regate, detail_exp_devise.num_expertise ...
                    FROM acores, infos_bureau, demande_expertise_dev, detail_exp_devise, agents
                        WHERE acores.id_acores = '$id_acores'
                        AND demande_expertise_dev.date_dem_exp = '$expdev' ...";

    $ergebnisT = mysql_query($abfrageT) or die("Query failed with error: ".mysql_error());

        while($abfrageT = mysql_fetch_assoc($ergebnisT))
        {
            $newcup         =   $abfrageT['coupure'];
            $newpd          =   $abfrageT['code_devise'];
            $newns          =   $abfrageT['num_serie'];
            $IDX            =   $abfrageT['id_dexpd'];
            $NumExp         =   $abfrageT['num_expertise'];

  echo"<tr>";
// Here are the ckeckboxes:
  echo"<td bgcolor=".$bgcolor." id='myGlob2' align='center'><input type='checkbox' name='check1' value='$abfrageT[id_dexpd]' /></td>";
  echo"<td bgcolor=".$bgcolor." id='myGlob2'>&nbsp;&nbsp;". $newcup ." ". $newpd ."</td>";
  echo"<td bgcolor=".$bgcolor." id='myGlob2' >&nbsp;&nbsp;". $newns ."</td>";
  echo"<td >&nbsp;&nbsp;</td>";      
  echo'</tr>';

And here the Code of the second page with my update:

$NumExp         = $_GET['NumExp'];
$IDX            = $_GET['check1']; 

$QryUpAnswerIdn = "UPDATE detail_exp_devise SET detail_exp_devise.exp_val  = 1
                WHERE detail_exp_devise.num_expertise = $NumExp
                AND id_dexpd = $IDX";
$ResUpAnswerIdn = mysql_query($QryUpAnswerIdn) or die("Query failed with error: " . mysql_error());

if ($IDX) {
$QryUpAnswerId = "UPDATE detail_exp_devise SET detail_exp_devise.exp_val  = 0
                WHERE detail_exp_devise.num_expertise = $NumExp
                AND id_dexpd = $IDX";
$ResUpAnswerId = mysql_query($QryUpAnswerId) or die("Query failed with error: " . mysql_error());   
}

Here a picture: enter image description here

Anybodu an idea?

THX in advance

achillix
  • 457
  • 4
  • 17
  • Why would you need to update a database to 0? Set the field to accept null values. You're also using the myGlob2 id 3x, id's should be unique, but that is aside from the question. If for some reason you absolutely must capture the 0's just do it in your update, if there is no value returned insert 0... although I still repeat that is completely irrelevant in a yes/no situation – Rick Calder Oct 28 '12 at 11:09
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Madara's Ghost Oct 28 '12 at 11:09
  • How do you create that list of checkboxes? If you have them stored in an object or an array beforehand, you can simply compare which value was send and which not. – insertusernamehere Oct 28 '12 at 11:11
  • I create the checkboxlist with the values from my Select Query. How can i realise a comparision? – achillix Oct 28 '12 at 11:20

2 Answers2

3

You cannot get the values of unchecked checkboxes. The browser would simply not submit them. There are workarounds (such as using <input type="hidden"> and updating it with JavaScript), but these feel very hacky and I wouldn't recommend you used them.

The simplest way, is to have two queries.

One will reset all values to 0, and the second will update those selected to 1.

In your example, add the following query before processing the form:

UPDATE detail_exp_devise SET detail_exp_devise.exp_val  = 0
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • I found your idea very good and i have tried to realize them, but it doesn't works. I have modified my question. If you can take a look please why it doesn't works? – achillix Oct 28 '12 at 11:37
  • @achillix: You seemingly misunderstood my point. **First**, you reset ALL the values in the database to 0. This way, it would start off as if nothing was set. **Only then**, you start updating the checkboxes which were set, and change those to one. You default them all to 0, then update. – Madara's Ghost Oct 28 '12 at 11:40
0

I do not know if this is best practice, but I send for every checkbox I use a hidden input with the same name and value 0. In this way, even if a checkbox is false, the hidden input with value 0 is send to the server.

In your case:

// Here are the ckeckboxes:
echo"<td bgcolor=".$bgcolor." id='myGlob2' align='center'>
  <input type='hidden' name='check1' value='0' />
  <input type='checkbox' name='check1' value='$abfrageT[id_dexpd]' />
</td>";
JvdBerg
  • 21,777
  • 8
  • 38
  • 55