-4

i am having following my sql field

table

`cbox1` tinyint(1) NOT NULL default '1',
`cbox2` tinyint(1) NOT NULL default '1',
`cbox3` tinyint(1) NOT NULL default '1',
`cbox3` tinyint(1) NOT NULL default '1',

index.html

<form method="post">
<input type="hidden" name="blah" value="blah">

<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">

<button type="submit">Submit</button>
</form>

demo.php

$sql="INSERT INTO freetrail (cbox1, cbox2, cbox3,cbox4)VALUES ('$cbox1', '$cbox2', '$cbox3','$cbox4')";

what I want to do is:

a) If the checkbox is checked, I want to update the appropriate field with 1

b) If the checkbox is unchecked, I want to insert the field with 0

How can i achieve my goal

Thanks in advance

shakti sharma
  • 47
  • 1
  • 2
  • 10

2 Answers2

0

First of all I would change these

    <form method="post">
            <input type="hidden" name="blah" value="blah">

            <input type="checkbox" name="cbox[]" value="1">
            <input type="checkbox" name="cbox[]" value="1">
            <input type="checkbox" name="cbox[]" value="1">
            <input type="checkbox" name="cbox[]" value="1">
            <input type="checkbox" name="cbox[]" value="1">

            <button type="submit">Submit</button>
    </form>

As you wont be able to tell severside which one is checked currently you'll get something like this if only one of them is checked, in the $_POST

  array(
       'blah' => 'blah', //your hidden field
       'cbox' => array(
            0 => 1
        )
  );

And if 2 are checked

   array(
       'blah' => 'blah', //your hidden field
       'cbox' => array(
            0 => 1
            1 => 1
        )
  );

But the question is which 2? So you will either need to change the names of these or the values

        <input type="checkbox" name="cbox[]" value="1">
        <input type="checkbox" name="cbox[]" value="2">
        <input type="checkbox" name="cbox[]" value="3">
        <input type="checkbox" name="cbox[]" value="4">
        <input type="checkbox" name="cbox[]" value="5">

OR

        <input type="checkbox" name="cbox1" value="1">
        <input type="checkbox" name="cbox2" value="1">
        <input type="checkbox" name="cbox3" value="1">
        <input type="checkbox" name="cbox4" value="1">
        <input type="checkbox" name="cbox5" value="1">

OR if you want to further complicate things

        <input type="checkbox" name="cbox[cbox1]" value="1">
        <input type="checkbox" name="cbox[cbox2]" value="1">
        <input type="checkbox" name="cbox[cbox3]" value="1">
        <input type="checkbox" name="cbox[cbox4]" value="1">
        <input type="checkbox" name="cbox[cbox5]" value="1">

Depending on what one you pick, you will need to then check if it is set, if the checkbox is not checked it wont be sent to the sever. There are a few ways around this depending how you name or do the values. The best approach is simply using

   $cbox1 = isset( $_POST['cbox1'] ) ? 1 : 0; //if you rename them
   ///or -- ( or something similar for each if you change the value )
   if( in_array( 1, $_POST['cbox'] )){
       $cbox1 = 1
   }else{
       $cbox1 = 0;
   }

Now which one you pick to do should depend on if the number of checkboxes is known, a fixed number, or dynamic. If it's a fixed number I would change the name ( the 2nd option ) as it is cleaner and will be much easier to deal with server side.

John Ruddell
  • 25,283
  • 6
  • 57
  • 86
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • this question is a duplicate. don't post answers to it. there are multiple posts with these exact same answers. please try to keep the community clean – John Ruddell Aug 21 '14 at 16:55
0

If HTML components are in these arrangement

<input type="checkbox" name="chkBx1" value="1">
<input type="checkbox" name="chkBx2" value="2">

in demo.php

function checkStatus($elementID) {
    if (array_key_exists($elementID, $_POST)) {
        $status = 1;
    } else {
        $status = 0;
    }
    return $status;
 }


 sql="INSERT INTO freetrail (cbox1, cbox2, cbox3,cbox4)VALUES ($this->chkEditStatus('chkBx1'),($this->chkEditStatus('chkBx1'), ($this->chkEditStatus('chkBx2'), ($this->chkEditStatus('chkBx3'),($this->chkEditStatus('chkBx4'))";
John Ruddell
  • 25,283
  • 6
  • 57
  • 86
Tharanga
  • 47
  • 1
  • 6