Checkbox unchecked value arrays

  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jul 18, 2008
  • Posts: 239
  • Loc: Western Australia
  • Status: Offline

Post July 10th, 2009, 6:17 am

I've got 5 checkboxes (days of working week) in an array which need to get posted to a php page

I check monday, tuesday, wednesday and friday for entry 0, and tuesday, wednesday & thursday for entry 1, and I get the following result:

Code: [ Select ]
[monday] => Array ( [0] => 1 )
[tuesday] => Array ( [0] => 1 [1] => 1 )
[wednesday] => Array ( [0] => 1 [1] => 1 )
[friday] => Array ( [0] => 1 )
[thursday] => Array ( [0] => 1 )
  1. [monday] => Array ( [0] => 1 )
  2. [tuesday] => Array ( [0] => 1 [1] => 1 )
  3. [wednesday] => Array ( [0] => 1 [1] => 1 )
  4. [friday] => Array ( [0] => 1 )
  5. [thursday] => Array ( [0] => 1 )


This is because the array doesn't start until the first checked value. Any solutions?

Edit: Would adding hidden values in front of entry 0 help?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 10th, 2009, 6:17 am

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 10th, 2009, 6:21 am

I'm not sure I understand what you're doing here, do you have any code surrounding the problem you could share?
I'd love to change the world, but they won't give me the source code.
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jul 18, 2008
  • Posts: 239
  • Loc: Western Australia
  • Status: Offline

Post July 10th, 2009, 6:27 am

Sorry, upon rereading it it doesn't seem very clear.

I've got a dynamic table with the javascript producing this (multiple times):
Code: [ Select ]
<input type="checkbox" name="monday[]" value="1"/
<input type="checkbox" name="tuesday[]" value="1"/>
<input type="checkbox" name="wednesday[]" value="1"/>
<input type="checkbox" name="thursday[]" value="1"/>
<input type="checkbox" name="friday[]" value="1"/>
  1. <input type="checkbox" name="monday[]" value="1"/
  2. <input type="checkbox" name="tuesday[]" value="1"/>
  3. <input type="checkbox" name="wednesday[]" value="1"/>
  4. <input type="checkbox" name="thursday[]" value="1"/>
  5. <input type="checkbox" name="friday[]" value="1"/>


When the first row isn't fully checked, the row after that appears as shown - "pushed up".

Please see my diagram below.
Attachments:
sophisticateddiagram.jpg
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 10th, 2009, 6:39 am

Ahh, ok I see the issue now. Browsers don't pass unchecked boxes to the server. Yor results are getting bumped up because it doesn't care about the blank values.

I would recommend building a javascript function that you call on submit that returns true/false - like validator functions. You could do something like this where you build a string from the values with a delimiter, throw that into a hidden input, and pass that.

Alternatively, you could also use onChange events to assign true/false to hidden inputs when your checkboxed are (un)checked. However, I think the first solution is a bit more elegant. If you'd like help coding it up, let me know.
I'd love to change the world, but they won't give me the source code.
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jul 18, 2008
  • Posts: 239
  • Loc: Western Australia
  • Status: Offline

Post July 10th, 2009, 6:52 am

Awesome, thankyou.

What I've got so far:

HTML Code: [ Select ]
<form id="form1" name="form1" method="post" action="settimetable.php" onSubmit="set_hidden_value()">
 
<input name="mondayhidden" type="hidden" value="toset" />
  1. <form id="form1" name="form1" method="post" action="settimetable.php" onSubmit="set_hidden_value()">
  2.  
  3. <input name="mondayhidden" type="hidden" value="toset" />


Javascript:
Code: [ Select ]
function set_hidden_value()
{
alert('Test');
var checkstring = "";
for (var i=0; i < document.form1.monday.length; i++)
{
if (document.form1.monday[i].checked)
     {
     checkstring = checkstring + "1";
     }
    else
    {
    checkstring = checkstring + "0";
    }
}
document.form1.mondayhidden.value = checkstring;
}
  1. function set_hidden_value()
  2. {
  3. alert('Test');
  4. var checkstring = "";
  5. for (var i=0; i < document.form1.monday.length; i++)
  6. {
  7. if (document.form1.monday[i].checked)
  8.      {
  9.      checkstring = checkstring + "1";
  10.      }
  11.     else
  12.     {
  13.     checkstring = checkstring + "0";
  14.     }
  15. }
  16. document.form1.mondayhidden.value = checkstring;
  17. }


But mondayhidden is still "unset", rather than "1101" etc. What am I doing wrong?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 10th, 2009, 7:26 am

I'm working up the code for you...hang tight.
I'd love to change the world, but they won't give me the source code.
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jul 18, 2008
  • Posts: 239
  • Loc: Western Australia
  • Status: Offline

Post July 10th, 2009, 7:39 am

Much appreciated. :)

//Unrelated - here's your display pic with a grey background (no ugly white rectangle)
Attachments:
21230 copy.gif
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 10th, 2009, 8:56 am

Sorry, got stuck on something at work...have a look at this:

alex89.php
Code: [ Select ]
 
<html>
    <head>
        <script language="JavaScript">
            function set_hidden_values()
            {              
                var days = new Array( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" );
                var cbVals = new Array( document.form1.mon, document.form1.tue, document.form1.wed, document.form1.thu, document.form1.fri );
               
                for(var i=0; i<cbVals.length; i++)
                {
                    var hiddenDay = document.getElementById(days[i]);
                    hiddenDay.value = "";
                    for(var j=0; j<cbVals[i].length; j++)
                    {
                        hiddenDay.value += (cbVals[i][j].checked) ? "1," : "0,";
                    }
                    hiddenDay.value = hiddenDay.value.substring(0, hiddenDay.value.length-1);
                }
               
                return false;
            }
        </script>
    </head>
    <body>
        <form name="form1" action="alex89_post.php" method="post" onSubmit="set_hidden_values();">
            <input type="hidden" id="Monday" name="Monday" value="" />
            <input type="hidden" id="Tuesday" name="Tuesday" value="" />
            <input type="hidden" id="Wednesday" name="Wednesday" value="" />
            <input type="hidden" id="Thursday" name="Thursday" value="" />
            <input type="hidden" id="Friday" name="Friday" value="" />
            <br />
<?php
            for($i=0; $i<3; $i++) {
                echo '<input type="checkbox" name="mon" />Monday
                    <input type="checkbox" name="tue" />Tuesday
                    <input type="checkbox" name="wed" />Wednesday
                    <input type="checkbox" name="thu" />Thursday
                    <input type="checkbox" name="fri" />Friday
                    <br />';
            }
?>
            <input type="submit" value="Submit" />
        </form>
    </body>
</html>
 
  1.  
  2. <html>
  3.     <head>
  4.         <script language="JavaScript">
  5.             function set_hidden_values()
  6.             {              
  7.                 var days = new Array( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" );
  8.                 var cbVals = new Array( document.form1.mon, document.form1.tue, document.form1.wed, document.form1.thu, document.form1.fri );
  9.                
  10.                 for(var i=0; i<cbVals.length; i++)
  11.                 {
  12.                     var hiddenDay = document.getElementById(days[i]);
  13.                     hiddenDay.value = "";
  14.                     for(var j=0; j<cbVals[i].length; j++)
  15.                     {
  16.                         hiddenDay.value += (cbVals[i][j].checked) ? "1," : "0,";
  17.                     }
  18.                     hiddenDay.value = hiddenDay.value.substring(0, hiddenDay.value.length-1);
  19.                 }
  20.                
  21.                 return false;
  22.             }
  23.         </script>
  24.     </head>
  25.     <body>
  26.         <form name="form1" action="alex89_post.php" method="post" onSubmit="set_hidden_values();">
  27.             <input type="hidden" id="Monday" name="Monday" value="" />
  28.             <input type="hidden" id="Tuesday" name="Tuesday" value="" />
  29.             <input type="hidden" id="Wednesday" name="Wednesday" value="" />
  30.             <input type="hidden" id="Thursday" name="Thursday" value="" />
  31.             <input type="hidden" id="Friday" name="Friday" value="" />
  32.             <br />
  33. <?php
  34.             for($i=0; $i<3; $i++) {
  35.                 echo '<input type="checkbox" name="mon" />Monday
  36.                     <input type="checkbox" name="tue" />Tuesday
  37.                     <input type="checkbox" name="wed" />Wednesday
  38.                     <input type="checkbox" name="thu" />Thursday
  39.                     <input type="checkbox" name="fri" />Friday
  40.                     <br />';
  41.             }
  42. ?>
  43.             <input type="submit" value="Submit" />
  44.         </form>
  45.     </body>
  46. </html>
  47.  


alex89_post.php
Code: [ Select ]
 
<?php
 
    $weekdays = array (
        "Monday" => null,
        "Tuesday" => null,
        "Wednesday" => null,
        "Thursday" => null,
        "Friday" => null,
    );
   
    foreach($weekdays as $key=>$val)
        if(isset($_REQUEST[$key]))
            $weekdays[$key] = explode(",",$_REQUEST[$key]);
   
    foreach($weekdays as $key=>$val) {
        echo $key.": ";
        foreach($val as $out) {
            echo $out." ";
        }
        echo "<br />";
    }
 
?>
 
  1.  
  2. <?php
  3.  
  4.     $weekdays = array (
  5.         "Monday" => null,
  6.         "Tuesday" => null,
  7.         "Wednesday" => null,
  8.         "Thursday" => null,
  9.         "Friday" => null,
  10.     );
  11.    
  12.     foreach($weekdays as $key=>$val)
  13.         if(isset($_REQUEST[$key]))
  14.             $weekdays[$key] = explode(",",$_REQUEST[$key]);
  15.    
  16.     foreach($weekdays as $key=>$val) {
  17.         echo $key.": ";
  18.         foreach($val as $out) {
  19.             echo $out." ";
  20.         }
  21.         echo "<br />";
  22.     }
  23.  
  24. ?>
  25.  


Live demo here.

UPDATE: edited code to include fix shown below.
I'd love to change the world, but they won't give me the source code.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 10th, 2009, 8:57 am

Quote:
Unrelated - here's your display pic with a grey background


Thanks. I've been too lazy to PS it myself, so I've just left it. It won't match the dark theme, but it's good for light theme. Avatars tend not to last too long for me, anways. :D
I'd love to change the world, but they won't give me the source code.
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jul 18, 2008
  • Posts: 239
  • Loc: Western Australia
  • Status: Offline

Post July 10th, 2009, 9:07 am

You're quite incredible, did you know?

Thanks a lot :)

In action: http://uwadb.com/timetable/setup.php?id=cal

Edit: If I go back and resubmit, it adds another few entries on the end of the key/array.

Submit, back, submit =
Monday: 1 0 01 0 0
Tuesday: 1 0 01 0 0
Wednesday: 0 1 00 1 0
Thursday: 1 0 01 0 0
Friday: 1 0 01 0 0

Any way to clear it?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 10th, 2009, 9:21 am

It's your browser cache adding to the hidden fields. You can modify the function to account for this if you would like:

Code: [ Select ]
 
function set_hidden_values()
            {              
                var days = new Array( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" );
                var cbVals = new Array( document.form1.mon, document.form1.tue, document.form1.wed, document.form1.thu, document.form1.fri );
               
                for(var i=0; i<cbVals.length; i++)
                {
                    var hiddenDay = document.getElementById(days[i]);
                    hiddenDay.value = "";
                    for(var j=0; j<cbVals[i].length; j++)
                    {
                        hiddenDay.value += (cbVals[i][j].checked) ? "1," : "0,";
                    }
                    hiddenDay.value = hiddenDay.value.substring(0, hiddenDay.value.length-1);
                }
               
                return false;
            }
 
  1.  
  2. function set_hidden_values()
  3.             {              
  4.                 var days = new Array( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" );
  5.                 var cbVals = new Array( document.form1.mon, document.form1.tue, document.form1.wed, document.form1.thu, document.form1.fri );
  6.                
  7.                 for(var i=0; i<cbVals.length; i++)
  8.                 {
  9.                     var hiddenDay = document.getElementById(days[i]);
  10.                     hiddenDay.value = "";
  11.                     for(var j=0; j<cbVals[i].length; j++)
  12.                     {
  13.                         hiddenDay.value += (cbVals[i][j].checked) ? "1," : "0,";
  14.                     }
  15.                     hiddenDay.value = hiddenDay.value.substring(0, hiddenDay.value.length-1);
  16.                 }
  17.                
  18.                 return false;
  19.             }
  20.  
I'd love to change the world, but they won't give me the source code.
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jul 18, 2008
  • Posts: 239
  • Loc: Western Australia
  • Status: Offline

Post July 10th, 2009, 9:26 am

Cheers :)

Also, just to be difficult :D

I've got all the other items arranged as follows:

Code: [ Select ]
$unitcode = $_POST["unitcode"];
#all other variables
 
for ($i = 0; $i < count($unitcode); ++$i) {
echo $unitcode[$i];
echo $category[$i];
#other variables
}
  1. $unitcode = $_POST["unitcode"];
  2. #all other variables
  3.  
  4. for ($i = 0; $i < count($unitcode); ++$i) {
  5. echo $unitcode[$i];
  6. echo $category[$i];
  7. #other variables
  8. }


Any way to easily rearrange from 5 rows of "0 0 1" etc to i rows of "1 0 1 0 0"?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 10th, 2009, 9:29 am

Can you clarify? Apparently I'm a little dense today. :)
I'd love to change the world, but they won't give me the source code.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 10th, 2009, 9:31 am

Do you mean that rather than seeing the arrays by days, you'd rather see them by the week (for the insert query)?

Update: If so, add this to the bottom of the processing php page:
Code: [ Select ]
 
$weeks = array();
   
    for($i=0; $i<count($weekdays['Monday']); $i++)
        $weeks[$i] = array (
            'Monday' => $weekdays['Monday'][$i],
            'Tuesday' => $weekdays['Tuesday'][$i],
            'Wednesday' => $weekdays['Wednesday'][$i],
            'Thursday' => $weekdays['Thursday'][$i],
            'Friday' => $weekdays['Friday'][$i]
        );
   
    foreach($weeks as $week) {
        foreach($week as $day => $val) {
            echo $day." - ".$val.", ";
        }
        echo "<br />";
    }
 
  1.  
  2. $weeks = array();
  3.    
  4.     for($i=0; $i<count($weekdays['Monday']); $i++)
  5.         $weeks[$i] = array (
  6.             'Monday' => $weekdays['Monday'][$i],
  7.             'Tuesday' => $weekdays['Tuesday'][$i],
  8.             'Wednesday' => $weekdays['Wednesday'][$i],
  9.             'Thursday' => $weekdays['Thursday'][$i],
  10.             'Friday' => $weekdays['Friday'][$i]
  11.         );
  12.    
  13.     foreach($weeks as $week) {
  14.         foreach($week as $day => $val) {
  15.             echo $day." - ".$val.", ";
  16.         }
  17.         echo "<br />";
  18.     }
  19.  


Demo updated.
I'd love to change the world, but they won't give me the source code.
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jul 18, 2008
  • Posts: 239
  • Loc: Western Australia
  • Status: Offline

Post July 10th, 2009, 9:33 am

Yeah, but I think I've got it. I'll let you know in 5
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 10th, 2009, 9:33 am

Post Information

  • Total Posts in this topic: 17 posts
  • Users browsing this forum: ScottG and 216 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.