RisingSun
RisingSun

Reputation: 1733

Multiple select fields PHP

I am working on a booking script in PHP and I need to get their values. To be clear the select is not multiple value select where you select many values in one select. It is just plain and simple select fields in a form. The problem is i can't be sure how many of them there are and what their names are. Is there a way to get their names and and values without knowing how many of them there are and what their names are when I click the submit button? I use a while loop to print them. I was suggested use sessions to achieve this but I am not exactly sure how I would implement it.

Upvotes: 0

Views: 179

Answers (2)

Thomas
Thomas

Reputation: 2984

All data that the html part is tranfering to the php part of the page is sent to a variable ($_GET, $_POST). Which one is defined by the method you are using for the form itself. The variable itself is built as an array with fieldname as the key of the array element and the value (either value of the selected element for select fields, or the input value for input fields, ....) as value of the array element.

If you want to parse through all elements that you have in the form field, then you can use foreach (it gets a bit more complicated if you don't want to parse through all elements......then I would suggest that you make sure that all the fields that you want to process begin with a specific "tag" in their name, which only they have. As an example c_Field1, c_Field2, Field3, Field4,.... with c_ being the tag....you can then either use string functions on the key (which is equal to the fieldname) or you can use arrayfunctions to only get these fields).

As an example with foreach if you want to get all fields beginning with 'c_':

foreach ($_POST as $fieldname => $value)
{
     if (str_replace('c_','',$fieldname)!=$fieldname)
     {
        //field to be processed so use $value
     }
}

Upvotes: 1

Nanne
Nanne

Reputation: 64429

If you submit them, they are in the POST variable (well, could be GET, but probably not). That thing is an array, so you can use all the fancy array functions to find out what's inside

for instance: http://php.net/manual/en/function.array-keys.php

or just a plain old foreach I guess.

Upvotes: 0

Related Questions