I just did something very similar to this on a page I'm working on. Try this :

while (list ($key, $val) = each ($the_array))
{
  echo "<input type=\"checkbox\" name=file[] value=\"$val\">"
          ."Some text relating to $val...";
 //etc ...
}

and

//inside some kind of if($_POST....) condition
$asize=sizeof($_POST['file']);
for($ii=0;$ii<$asize;$ii++)
{
some operation on $_POST['file'][$ii]
}

    Ive tried declaring the checkbox name as an array

    <input name=list[] type="checkbox" value="'.$ref_id.'">

    but I get error on page message when I do it this way

      Truthfully, I'm confused on your tag syntax:
      value="'.$ref_id.'">

      I'm pretty sloppy with quotes, especially when the HTML lets me get away with it! As a consequence, I don't know all the ins and outs of single and double quote combinations - just that sometimes they don't work as they theoretically should.

      if you are echo-ing the statement, just escape the quotes (\") and try echo"<input name=\"list[]\" type=\"checkbox\" value=\"$ref_id\">";

      If your code is not echo'd (i.e. in a plain HTML section of the page), try this:

      <input name="list[]" type="checkbox" value="<?echo$ref_id?>">

        Yeah I see what your getting at but I made sure it wasnt a quotes related thing, I tried all sorts of combinations and I viewed the source code of the html page produced by php and it looks fine

          try this:

          <input name="list" type="checkbox" value="<?php echo $ref_id; ?>" <?php if($_POST['list'] == $ref_id){echo "selected";}>

          see if it works..

            Originally posted by EvoRacer
            try this:

            <input name="list" type="checkbox" value="<?php echo $ref_id; ?>" <?php if($_POST['list'] == $ref_id){echo "selected";}>

            see if it works..

            Thats not quite what Im looking for, Im gonna put more of the code up to try clarify

            1: a little javascript to let me check/uncheck all the boxes at once

            <!-- Begin
            function checkAll(field)
            {
            for (i = 0; i < field.length; i++)
            field.checked = true ;
            }

            	function uncheckAll(field)
            	{
            		for (i = 0; i < field.length; i++)
            		field[i].checked = false ;
            	}
            	//  End -->
            	</script>

            2: call some info out of a database and assign a value for each checkbox

            for($x=0;$x<$noofRows;$x++)
            {
            $ref_id= mysql_result($result,$x,'id');
            echo '<input name=list type="checkbox"value="'.$ref_id.'">';
            }

            Call the Javascript to either check or uncheck all the boxes

            <a href = "javascript:checkAll(document.myForm.list)">select all</a> / <a href = "javascript:uncheckAll(document.myForm.list)">unselect all</a></td>

            This all works a treat, but how do i now capture the selected checkboxes when I submit the form?

              If it is selected, it will be added to the $POST array. In your case, it each item will be in the $POST

              • array: more specifically $_POST

                • [0:n-1] where n is the size of the array.

                  try this on the page that receives the POST vars...:
                  if($POST

                  • !=NULL)
                    print_r($_POST

                    • );


                      If all is well, you should get something that looks like this:
                      Array( [0] => value1 [1] => value2 ...)

                      With this knowlege, use the for statement to iterate through each item of the array:
                      $asize=sizeof($POST

                      • );
                        for($x;$x<$asize;$x++)
                        {
                        echo $_POST

                        • [$x];
                          }

                          Hope this helps!

                Originally posted by cnperry
                If it is selected, it will be added to the $POST array. In your case, it each item will be in the $POST

                • array: more specifically $_POST

                  • [0:n-1] where n is the size of the array.

                    try this on the page that receives the POST vars...:
                    if($POST

                    • !=NULL)
                      print_r($_POST

                      • );


                        If all is well, you should get something that looks like this:
                        Array( [0] => value1 [1] => value2 ...)

                        With this knowlege, use the for statement to iterate through each item of the array:
                        $asize=sizeof($POST

                        • );
                          for($x=0;$x<$asize;$x++)
                          {
                          echo $_POST

                          • [$x];
                            }

                            Hope this helps! [/B]



                This is along the lines of what I did originally, the problem now is that this will work if I declare the checkboxe list as 'list[]' i.e

                echo '<input name="list[]" type="checkbox" value="'.$ref_id.'">';

                but by doing this the javascript to select/unseilect all of the boxes wont work. It will only work if I name the checkbox group 'list' without the bracketst i.e.

                echo '<input name="list[]" type="checkbox" value="'.$ref_id.'">';


                see what I mean??

                  For the javascript to check all checkboxes:

                  function checkAll() {
                          for (i=0,n=document.form1.elements.length;i<n;i++){
                                  if (document.form1.elements[i].name.indexOf('usr[]') !=-1){
                                        document.form1.elements[i].checked = true;
                                  }
                          }
                  }
                  

                  then you'd have your checkboxes:

                  <input type="checkbox" name="usr[]" value="<? echo $ref_id; ?>">
                  

                  a button to toggle select all:

                  <a href="javascript:void(0);" onClick="checkAll();">check all</a>
                  

                  as for the reference page:

                  <?
                  foreach ($usr as $line => $text){
                                  echo $line . " => ". $text ."<br>\n";
                          }
                  ?>
                  

                    Originally posted by Prikid
                    For the javascript to check all checkboxes:

                    function checkAll() {
                            for (i=0,n=document.form1.elements.length;i<n;i++){
                                    if (document.form1.elements[i].name.indexOf('usr[]') !=-1){
                                          document.form1.elements[i].checked = true;
                                    }
                            }
                    }
                    

                    then you'd have your checkboxes:

                    <input type="checkbox" name="usr[]" value="<? echo $ref_id; ?>">
                    

                    a button to toggle select all:

                    <a href="javascript:void(0);" onClick="checkAll();">check all</a>
                    

                    as for the reference page:

                    <?
                    foreach ($usr as $line => $text){
                                    echo $line . " => ". $text ."<br>\n";
                            }
                    ?>
                    

                    [/B]

                    nope still gives me an error on page for the javascript, the php section catchs the individual checkboxes if I manually check them but when I use the call the checkAll function it dosnt work

                    :queasy: :bemused:

                      make sure your 'form' tag has 'name' value set to 'form1'

                      <form name="form1">
                      

                        Legend.......... Works Perfectly!🙂 🙂

                        Thanks to everyone else who contributed also!

                          Write a Reply...