Guys,

My ISP (Dreamhost) has Register Globals turned off, and I have a page of several checkboxes with the same name but differing values, upon the page being submitted I need to be able to loop through these checkboxes within a single variables name.

I've searched and searched for all results seem to show solutions using Register Globals on, how can I get around this being turned off without using Javascript to collate the array clientside?

Thanks!

    Here's a test script to demonstrate:

    $check_options = array('option_a', 'option_b', 'option_c');
    if (!isset($_POST['submit'])) {
        echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
        foreach ($check_options as $opt) {
            echo '<input type="checkbox" name="checkbox[' . $opt . ']" />';
        }
        echo '<input type="submit" name="submit" /></form>';
    } else {
        print_r($_POST);
        foreach ($check_options as $opt) {
            echo '<br />' . $opt;
            if (isset($_POST['checkbox'][$opt])) {
                echo ' was checked';
            } else {
                echo ' was not checked';
            }
        }
    }

      Just to clarify here, each checkbox input element must either have a different name (including the array notation as used in Installer's example) or else the name must end in "[]" (which will create an enumerated array of values). If not, then only the last one in the form that is checked will be transmitted to your script. If you use the "name[]" convention, e.g....

      <form action="" method="post">
      <input type="checkbox" name="test[]" value="1" />1
      <input type="checkbox" name="test[]" value="2" />2
      <input type="checkbox" name="test[]" value="3" />3
      <input type="submit" name="submit" />
      </form>
      

      ...then you could loop through them with...

      echo "You checked:<br />";
      foreach($_POST['test'] as $value)
      {
         echo $value . "<br />";
      }
      

        Some further clarification 🙂 :

        When you use array notation in the checkbox fields, only those boxes that are actually checked will be sent to the receiving script. If no explicit keys are provided in the "value" attribute, then the resulting array will be indexed "0, 1, etc." with index 0 assigned to the first box that was checked, not the first box in the select element.

        If the boxes are explicitly keyed, then the sent checkbox array will be similarly keyed. In either case, again, only those that have been checked will be in the array.

        So if you want to loop through the checkbox array you should first test with, e.g., if (!empty($_POST['checkbox'])), since the array will not be set if nothing was checked.

          Many thanks for your help guys, this worked beautifully!!

          So does having Register Globals on simply automate the assigning of GET/POST variables to PHP variables?

            Guys,

            A problem has arisen since doing this - I have links on the page to SELECT ALL or SELECT NONE for the checkboxes, however since adding the [] to the end of the checkbox name, I'm getting a Syntax error when calling this Javascript....

            function Ash_CheckAllCheckboxes(field)
            {
            for (i = 0; i < field.length; i++)
            field.checked = true ;
            }

            	function Ash_UnCheckAllCheckboxes(field)
            	{
            	for (i = 0; i < field.length; i++)
            		field[i].checked = false ;
            	}[/QUOTE]

            Any ideas how I can amend this script to take account of the [] without prompting a syntax error?

              a month later

              Got another issue with this guys, I somehow want to pass the $_POST parameter of the Checkboxes into a PHP function that then outputs a string with delimiters.

              Here's my function

              function Ash_ConvertArrayToDelimited($TheArrayToSplit, $TheDelimiter) {
              
              foreach($TheArrayToSplit as $TheValue) {
              	if (is_numeric($TheValue)) {
              		$TheDelimitedString = $TheDelimitedString . $TheValue . $TheDelimiter;
              	}
              		return $TheDelimitedString;
              }
              }

              And here's the line I'm running in the page itself (as the functions are an Included/Require_Once file)

              $TheProfileLookingForID = htmlentities(trim(Ash_ConvertArrayToDelimited($_POST['ProfileLookingForID'],'|')));

              For some reason, I only get the contents of the FIRST checkbox and one delimiter.

              Appreciate your help!

              Any ideas?

                Just to add, I do have the [] on the end of the checkbox HTML name, so its being passed as an array, the problem seems to be in passing the array to the function?

                  Write a Reply...