Jump to content

Converting from ASP to PHP help.


TapeGun007

Recommended Posts

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]

Link to comment
Share on other sites

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";
    }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.