Checkboxes ...How To get Checked Status??
Results 1 to 4 of 4

Thread: Checkboxes ...How To get Checked Status??

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    32

    Checkboxes ...How To get Checked Status??

    Greetings June 19th 2013

    Below I have allowed a couple of lines of code that represent my attempts to get a dozen check boxes state of either "True" or "False" as is easily done in Javascript. However, after an hour of attempts all I have gotten is the check box value. I have tried them as an array while I usually uniquely label them as numerically incrementing variables and generate the index. But I realize the array is far easier... Any ideas? any help is greatly appreciated :-)


    for($i=0; $i <=12; $i++)
    {
    fwrite($file,$_REQUEST['ckbx[$i]'] . "\n");
    }
    //fwrite($file,$_REQUEST['ckbx[1].checked']."\n");

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,396
    What does the HTML markup look like for the checkboxes? Assuming you're using names like "ckbx[0]", "ckbx[1]", etc. then $_REQUEST['ckbx'] will be an array; this:

    PHP Code:
    $_REQUEST['ckbx[$i]'
    won't exist for two reasons:
    1. Variable interpolation doesn't occur within single quote delimited strings, thus you're telling PHP to look for an array index of "ckbx," followed by a left square bracket, followed by a dollar sign, followed by... etc.
    2. $_REQUEST['ckbx'] itself is the array; when you want to access a specific index in that array, the square brackets go on the end (not inside the key of the outer array, $_REQUEST).


    Second, if checkboxes aren't checked, then they won't be included in the external data at all (since the user's browser won't send anything for those elements). Thus, you can use isset() to check if the given index in the array exists; if it does, the checkbox was checked, and if it doesn't, then it wasn't checked.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    32
    bradgrafelman, June 20th, 2013

    Below is the revision I believe you suggested and it did work, partially. I am able to get a "Value" of 11 of the elements however even after altering the indexes of the html elements i.e "name="ckbx[1]" I get all but the first element that reports the word "ON" which I interpreted as the fact that check boxes were selected. Much appreciated, your help was extremely useful :-)

    for($i=0; $i <=12; $i++)
    {
    fwrite($file,$_REQUEST[ckbx][$i] . "\n");
    echo "test ".$i."===>".$_REQUEST[ckbx][$i];
    }

  4. #4
    Senior Member
    Join Date
    Mar 2009
    Posts
    802
    You should be able to simply use isset() to check if a checkbox has been checked or not.

    PHP Code:
    if(isset($_POST['checkbox_name_here']))
    {
        
    //checked
    }
    else
    {
        
    //not checked

    Declare variables, not war.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •