0

I have a form with a couple of text areas and then about 20 input fields.

The input fields are created dynamically, using a loop and are created from values in my db (countries)

My code is:

$options = '';
$country_code = '';
$query = $DB->query("SELECT country_code, country_id, IF(country_code = ".$country_code."', '', '') AS sel FROM exp_sme_countries WHERE site_id='".$this->settings['site_id']."' ORDER BY country_name ASC");
           foreach ($query->result as $row)
           {
            $options .= '<label>' . 'Phrase for ' .  $this->settings['countries'][$row['country_code']] . '</label>' . '<br />';
                            $options .= '<input style="width: 100%; height: 5%;" id="country_data" type="text"  name="' . $row['country_id'] . '"  />' . '<br /><br />';
                            $options .= '<input type="hidden" name="country_id" id="country_id" value="' . $row['country_id'] . '"   />';

           }

This outputs examples such as:

input style="width: 100%; height: 5%;" id="country_data" type="text"  name="68"  />
input style="width: 100%; height: 5%;" id="country_data" type="text"  name="28"  />

Now my problem is, how do I get the values of these input fields?

I have looked at outputting $_POST, but this seems to return data that I can't really access.

Can these values be accessed in any way?

Or do I need to change the way I'm doing things?

Thanks

1
  • 1
    It looks like you're outputting duplicate html ids - don't do that. Commented Nov 19, 2010 at 16:50

4 Answers 4

1

change your 'name' of the fields as follows:

name="p_' . $row['country_id'] . '"

then you can go:

    foreach ($_POST as $key => $value) {
        if (substr($key, 0, 2) == "p_") {
            $country_id[str_replace("p_", "", $key)] = $value;
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to work. I just need to plug it into a loop to get everything into my db tables. Thanks!
0

like said above, you do have

name="country_id"

probably like 70+ times as well. That's going to be a problem.

You can do:

name="country_id[]"

and get an array in PHP after it gets POSTed.

Also, are you sure you are POSTing? Seems not if $_POST is empty..

Comments

0

$_POST[68] and $_POST[28] will return the values you need. You should use alpha keys (country_code) instead. If for some reason those numeric IDs in your database change, you'll run into trouble maintaining your code. HTML also requires you to use unique element IDs for a document to be valid.

Comments

0

Your best solution would be to create an array with the HTML so it posts in the format you want... refer to this post for an example: PHP: help with array structure

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.