tags:

views:

94

answers:

3

I have a form that has a field pulled from the database as a dropdown. I need to get the text selected in the dropdown but I don't know in advance what the field ID will be.

This is basically just a form that has already been generated. I don't need to pull anything from the database, it's already on this page. All I need to do is get the form information and email it, no writing to the database.

I know how to do the _Request for the other fields based on the ID but I'm not sure how to do this one. The ID changes. It can be ID=1, ID-2, etc.

I need to do something like: _REQUEST form element where ID is LIKE "ID[*]" or something similar.

Any suggestions or links to tutorials?

Here are a couple samples of what the dropdown renders on the page:

<div class="wrapperAttribsOptions">
<h4 class="optionName back"><label class="attribsSelect" for="attrib-  1">Model</label></h4>
<div class="back">
<select name="id[1]" id="attrib-1">
    <option value="45">VC3-4C</option>
    <option value="1">VC3-4PG</option>
    <option value="3">VC3-4SG</option>

<div class="wrapperAttribsOptions">
<h4 class="optionName back"><label class="attribsSelect" for="attrib-14">SPK   Model</label></h4>

<div class="back">
<select name="id[14]" id="attrib-14">
    <option value="43">SPK-4</option>
    <option value="44">SPK-8</option>
</select>

TIA

+1  A: 

EDIT: Scott Saunders is right, PHP will automatically create an array for you. If for some reason his code doesn't work, the below might still apply (could differ based on PHP configuration).

The easiest way would be using a regular expression:

foreach($_REQUEST as $key => $value)
{
    if(preg_match("/id\[[0-9]*\]/", $key))
    {
        // $key now contains the name, and $value the value.
    }
}

This is assuming the id will always be numerical.

Arda Xi
so would the 0-9 mean any/all digits? Would that allow for two and three digits? (14, 15, 300, etc)?
Yes, that's what the * does. It means: "match the preceding bit (in this case `[0-9]`) between zero and unlimited times." If you only want between 1 and 3 you can use `"/id\[[0-9]{1,3}\]/"`For more information, check out http://www.regular-expressions.info/ .
Arda Xi
Thanks. I'll give it a try this evening. But this sure looks like what I need.Thanks again!
+1  A: 

The brackets [] mean that PHP will create an array variable with the posted values. So you should have $_REQUEST['id'] as an array. There should be a value keyed by the number in the form ($_REQUEST['id'][1] in your first example, $_REQUEST['id'][14] in your second).

If there will just be one value (you don't say what you're trying to do with these values) then you can just shift it off the array.

$value = array_shift($_REQUEST['id']);

If there will be multiple values, you can loop through them:

foreach($_REQUEST['id'] as $number => $value) { ... }
Scott Saunders
Oh right, completely forgot about that feature.
Arda Xi
Like I said, all I need to do is gather the text once the selection has been made and email it. I'll look into this one as well.
Using `array_shift` is definitely less costly performance-wise, seeing as regexes are pretty slow.
Arda Xi
Thanks! Worked great. And now I'm a happy guy!
A: 

If there is a large number of fields or the id values cannot be known, I would follow Arda's answer.

The alternative is to interrogate the id collection directly:

$id_1 = $_POST['id'][1];
$id_14 = $_POST['id'][14];

All items posted to the server with square brackets in their name are converted to an array. Having already specified the index, as you have done, may be more beneficial depending upon ones needs.

KDrewiske
There are quite a few fields, but I think only this one uses numerals. All the others are identified by name/string.The id value won't be known in advance. It's based on the product they select. These are attributes tied to the product.