Jump to content

Checkbox values not being treated as an array


Paul_B

Recommended Posts

New here and baffled after a lot of forum-searching (but hoping I'm overlooking something obvious nonetheless... ).

 

I have a form with some identically named checkboxes with the requisite "[]" at the end of the name (as follows in relevant part...):

 

 

<form action="/pathto/filename.php" method="post">
<p><strong>Access To:</strong><br />
Family Law Clinic <input type="checkbox" name="ctr"  value="FLC[]" checked /><br>
Pro Bono Center <input type="checkbox" name="ctr"  value="PBC[]" checked /><br>
Juvenile L. & Policy Clinic <input type="checkbox" name="ctr"  value="JLP[]" checked /><br>
Disability Law Clinic <input type="checkbox" name="ctr"  value="DLC[]"  />[/sELECT]<br>
<input name="submit" type="submit" value="Update" />
</form>

 

(The "checked" designations are there based on a database check of the user's current status, which is what this form is designed to edit.)

 

What I want and expect on the server end is an array containing any checked values submitted, which I will then implode into a single constant for the database.  What PHP yields, no matter what sort of operation I try, is a value equal to the last selected value of the four boxes.  For example:

 

if(is_array($_POST['ctr']))
{
$ctr = $_POST['ctr'];
    foreach($ctr as $value)
    {
        echo "{$value}<br />";
    }
}

else {
    echo "This is not being treated as an array: {$_POST['ctr']} ";
       }

With this coding, the "is_array" test determines the my checked values are not an array and doesn't take it through the foreach loop and the "This is not being treated as an array: {$_POST['ctr']} "; echoes with the variable as the last checked box value.

 

If I force it through the loop, I get a blank screen and the PHP warning "Invalid argument supplied for foreach() ..."

 

So it's pretty clear to me that PHP is not treating the "ctr[]" checkbox values as an array.  But I'm darned if I can figure out why not.

 

Ideas?

 

Thanks in advance, ]

 

Paul

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.