Jump to content

getting matching post values from input fields


terrid25

Recommended Posts

Hi

 

I have a form that is to input data from input fields to a table.

The form is as follows:

                            $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="country_id[]"  />' . '<br /><br />';
                               $options .= '<input type="hidden"  name="country_data[' . $row['country_id'] . ']" value="'.$row['country_id'].'"  />';
                            }

 

This outputs:

<input style="width: 100%; height: 5%;" id="country_data" type="text"  name="country_id[]"  />
<input type="hidden"  name="country_data[68]" value="68"  />

<input style="width: 100%; height: 5%;" id="country_data" type="text"  name="country_id[]"  />
<input type="hidden"  name="country_data[28]" value="28"  />

 

What I need to do, is get the values from the input fields and then match them with the correct hidden field value

 

So if in the first input field, I typed in 'Test data' and the second input, i typed 'This is great'

 

On the POST, I need to find the the value ('Test data') and also the value from the hidden field.

 

So something like:

The text you entered is: Test data with hidden value of 68

The text you entered is: This is great with hidden value of 28

 

I'd rather not use $_GET as this is a form that inserts data into a database.

 

Could someone point my in the right direction or provide an example?

 

Link to comment
Share on other sites

Don't give the hidden input an index so it should be the same as the text input:

 

<input style="width: 100%; height: 5%;" id="country_data" type="text"  name="country_id[]"  />
<input type="hidden"  name="country_data[]" value="68"  />

<input style="width: 100%; height: 5%;" id="country_data" type="text"  name="country_id[]"  />
<input type="hidden"  name="country_data[]" value="28"  />

 

$text = $_POST['country_id'];
$data = $_POST['country_data'];

foreach($text as $key => $value) {
   echo "The text you entered is: $value with hidden value of " . $data[$key];
}

Link to comment
Share on other sites

I don't see the need for the hidden field at all, just use the value as the country_id[...] index -

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

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.