Paul_B Posted July 26, 2010 Share Posted July 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/208952-checkbox-values-not-being-treated-as-an-array/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 26, 2010 Share Posted July 26, 2010 Use - name="ctr[]" Quote Link to comment https://forums.phpfreaks.com/topic/208952-checkbox-values-not-being-treated-as-an-array/#findComment-1091415 Share on other sites More sharing options...
Paul_B Posted July 26, 2010 Author Share Posted July 26, 2010 Use - name="ctr[]" Ouch--I can't believe how long I looked at those bracketed values instead of names. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/208952-checkbox-values-not-being-treated-as-an-array/#findComment-1091420 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.