Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to check if $_REQUEST element is set

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
CA
Hello,

I have a page that has a set of checkboxes that are created dynamically. In other words each of the checkboxes is assigned a name dynamically by appending a number to the string "box". The problem that I'm having is that when the form is submitted I want it to do something in particular depending on what checkbox is selected. I have tried using the following code
Code:
if(isset($_REQUEST['HASCUSTOMBRANCHES']))
{
	$_SESSION['CustomBranches'] = "";
	$HASCUSTOM = 1;
	
	for($i = 1; $i <= $_REQUEST['HASCUSTOMBRANCHES']; $i++)
	{
                print "inside for loop";
		$checkBox = "box$i";
                //if($_REQUEST('$checkBox')
		if(isset($_REQUEST["$checkbox"]))
		{
			print "the checkbox $checkBox was checked";
		}
	}
}

but it doesn't seem to be able to detect that the checkbox has been checked. I know the request variable (i.e. box2) exists since I did a
Code:
print_R($_REQUEST);
and saw it listed. So my question is how do I get PHP to recognize that the particular box REQUEST variable has been assigned a value? Any and all help is appreciate.

Thanks
 
The state of the checkbox is not relevant in this case... since if the checkbox was passed from the html page to your script, then it was 'checked' by the user.

Unlike radio buttons that have on/off states, a checkbox is either checked (included in the form data that is submitted) or not checked (and so not included when submitted).

Cheers,
Jeff


[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Thanks for the tip Jeff. What I want to do though is interate through an array which has as it's upper bound the number of checkboxes that exist. With each iteration I want to check to see if the checkbox being referenced (i.e. box4) has been set and if it has then append the value onto the end of a string/list..or someother construct. So while only the checkboxes that have values assigned will be send with the request I still need to check all the possible checkboxes to see if they have been assigned a value. Can you recommend any ways I could accomplish this?

Thanks
 
Can you post the code you're using to generate the form? And the resultant HTML?

That would give us a hint as to what's going on here.

Ken
 
Sure,

Here is the code that generates the checkboxes.

Code:
print "<p style=\"line-height: 1.5em;\">SELECT CUSTOM BRANCHES:</p>
    <table bgcolor=\"#f5f5f5\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">
    	<tr>";
    		$lc = 1;
    		$colc = 0;

    		while($record = mysql_fetch_object($result))
    		{
    			$branch = $record -> Branch;
    			$description = $record -> brdesc;
    			print "setting checkbox to box$lc";
    			print "<td><input type=\"checkbox\" name=\"box$lc\" value=\"$branch\">" .
    					"$description </td>";
    			$lc++;
    			$colc++;
    			if($colc == 4)
    			{
    				print "</tr>";
    				print "<tr>";
    				$colc=0;
    			} 	
    		}
  ?>  		
		</tr>
	</table>

And here is the code that is supposted to iterate through all the checkboxes and check to see if each has been selected. Note HASCUSTOMBRANCHES is a hidden field that stores the number of checkboxes. This value is being retrieved correctly.



Code:
for($i = 1; $i <= $_REQUEST['HASCUSTOMBRANCHES']; $i++)
	{
		$checkBox = "box$i";
		print "$checkBox";
		//if(isset($_REQUEST["$checkbox"]))
		if($_REQUEST[$checkbox])
		{
			print "the checkbox $checkBox was checked";
		}
	}


Any ideas?
 
Ok, the easiest solution is to use a array for the name of the checkbox.

Code:
echo '<p style="line-height: 1.5em;">SELECT CUSTOM BRANCHES:</p>
    <table bgcolor="#f5f5f5" width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>';
            $lc = 1;
            $colc = 0;

            while($record = mysql_fetch_assoc($result))
            {
                print '<td><input type="checkbox" name="box[]" value="' . $record['branch'] . '">' . $record['brdesc'] . '</td>';
                $lc++;
                $colc++;
                if($colc == 4)
                {
                    echo '</tr>';
                    echo '<tr>';
                    $colc=0;
                }     
            }
  ?>          
        </tr>
    </table>
You'll notice I changed more than a few lines. For some reason I like echo more than print, but that's my own style. :)

I changed the double quotes to single quotes so you don't have to escape the double quotes. I think it makes the source code easier to read.

The name of each checkbox generated is "box[]". When the form is submitted there will be an array in $_POST named "box". The size of the array will be the number of boxes checked. So your processing code should look like this:
Code:
for($i = 1; $i <= count($_POST['box']; $i++)
      echo $_POST['box'][$i] . ' was checked<br>'."\n";

See if this helps.

Ken
 
Thanks guys,

I actually figured out the reason why I was having the problem. I was referencing "checkbox" instead of "checkBox" <- notice the uppcase "B". Anyways that's the reason why the if statement wasn't detecting that the checkbox had been selected. Is there anyway to turn on some sort of warnings or notices when you're trying to reference a variable that hasn't been decalred?
 
Yes there is, check the php manual under error checking. That's one of the reasons I use all lowercase for all of my variable. The other reason that it's easier to type.

You probably still should look into using arrays to hold the checkbox values. It makes code much easier to write and debug. I know when I started creating form like yours, I also used the same mechanism for creating multiple names. When started using arrays, my coding went much quicker.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top