I need to post several values from a html form into a php script.

The form uses multiple checkboxes
ie

<input name="month" type="checkbox" id="month" value="8">Aug<BR />
<input name="month" type="checkbox" id="month" value="9">Sept<BR />

The problem here is that it posts the variable "$month" with the latest value ... ie "9" if both boxes are checked. I need to pass both these values to the script.

I know that an obvious solution would be to name the checkboxes differently, eg month8 and month9 but these checkboxes need to be of the same name because a java script is handling a "check all" feature.

Is there a way I can get this form to pass all the checked values as an array of $month or similar solution?

Thanks ... Fizz

    Hi,

    You can give all your checkboxes the name month[]. That way, you will get all the checked months in an array when the form is submitted.

    Yours, Erik.

      Ahhhh ... obvious once pointed out to you

      Thanks alot, that works just as required! ... Fizz

        a year later

        sorry to dig up this old thread but im having problems trying to get the data out of the array.

        i got this code in the form

           <input type=checkbox name=functions[0] value=accounting> Accounting<br>	
        	 <input type=checkbox name=functions[1] value="word processing"> Word Processing<br>	
        	 <input type=checkbox name=functions[2] value="Sales Aids"> Sales Aids	<br>
        	 <input type=checkbox name=functions[3] value="client policy information"> Client Policy Information<br>
        	 <input type=checkbox name=functions[4] value=applications> Applications<br>
        	 <input type=checkbox name=functions[5] value=claims> Claims		<br>
        	 <input type=checkbox name=functions[6] value="rate/quote"> Rate/Quote	<br>	
        	 <input type=checkbox name=functions[7] value=other> Other functions (Be specific)

        in the php code i post the data to, i push the post array onto a stack with this.

        $count = 0;
        foreach ($_POST as $key => $value) { //take name&data pair 
          array_push($stack, $key, $value);//and put it onto the stack
          echo $count.": ".$key." - ".$value."<br>";
          $count++;
        }
        

        I'm inserting the data into mysql. I'm using the following function to create a insert query.

        function insert($x, $stack, $table) {// x equals how many values to pull off the stack, stack is the stack array, and table is a string for which table to insert to 
        $query = "insert into ".$table." set ";//move data into table
        for ($i=0;$i<$x;$i+=2) {//Get data
          if (substr(pos($stack), 0, 4) == "com_")  // is field name prefixed with com_?
            $query = $query.substr_replace(pos($stack),"",0,4)."='";//Get field name shave off com_
          else 
            $query = $query.pos($stack)."='";//Get field name
          next($stack);  echo "<BR>-".pos($stack)."-<BR>";
          if (pos($stack) == "Array") { // Is the data in an array? (checkboxes)
            echo "in<BR>"; prev($stack); // get array name
            foreach(pos($stack) as $element) 
              $query = $query.$element.","; // add all set checkboxes as data
            $query= $query."',";// finish query statement
            next($stack); next($stack);  //move to next data pair
          } else {
          $query= $query.pos($stack)."',";//Get data entry
          next($stack); // move to next data pair
          }
          //echo $i."<br>";
        }
        $query = substr($query,0,(strlen($query)-1));//shave last comma
        return $query;
        }
        

        my problem is i can't figure out a way to identify an array and add the field names & values to the query string.

        i try to identify the array by using

        if (pos($stack) == "Array") { // Is the data in an array? (checkboxes) 

        but the expression never goes true even though the echo before this statement outputs Array. Also, im almost sure the foreach inside that if statement will not execute as i think it should.

          Maybe the is_array() function can help...

          PS: you can start YOUR own thread next time for no additional cost to you...

            Write a Reply...