TapeGun007 Posted June 17, 2010 Share Posted June 17, 2010 I have a database that contains several "Groups" that people belong to. My web page reads the database and spits out the groups onto the page. The form allows you to select which groups you want and passes this information onto the next page. (see picture) The form you see in the picture simply uses the input tag Checkbox. The checkbox is named in sequential order, so... Group1 = Choir Group2 = Chorale Group3 = Directors ..etc In classic ASP, the next page would read how many groups were selected with this code: Dim a For a = 1 to Request.Form("GroupName").Count Response.Write(Request.Form("GroupName")(a) & " was selected<BR>") Next How can I do this same thing in php? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/205100-converting-from-asp-to-php-help/ Share on other sites More sharing options...
Psycho Posted June 17, 2010 Share Posted June 17, 2010 Instead of naming the checkboxes in sequential order, they should be named as an array <input type="checkbox" name="groups[]" value="Choir" /> <input type="checkbox" name="groups[]" value="Chorale" /> Then in the processing page you would just do this if(isset($_POST['groups'])) //Only run if checkboxes were selected { foreach($_POST['groups'] as $groupName) { echo "{$groupName} was selected<BR>\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/205100-converting-from-asp-to-php-help/#findComment-1073629 Share on other sites More sharing options...
TapeGun007 Posted June 17, 2010 Author Share Posted June 17, 2010 Ah yes, that works perfectly. I was getting closer and closer to the answer. I knew there was a foreach command, just wasn't sure how. Plus, I didn't know you could simply pass the array like that. That's great! Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/205100-converting-from-asp-to-php-help/#findComment-1073636 Share on other sites More sharing options...
Psycho Posted June 17, 2010 Share Posted June 17, 2010 Plus, I didn't know you could simply pass the array like that. Yeah, I'm pretty sure that works for ASP as well, but it's been years since I used it. The one thing to be aware of with that is that referencing those fields in JavaScript is somewhat counter-intuitive. They are still an array, but not how you might think. Example: var checkboxGroup = document.forms['formname'].elements['groups[]']; Note the use of "[]" in the referenced name. Quote Link to comment https://forums.phpfreaks.com/topic/205100-converting-from-asp-to-php-help/#findComment-1073642 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.