Jump to content

Recommended Posts

Hi all

 

I have a form with a few textareas and a bunch of input fields. The input fields are dynamically created from querying a country table.

 

		   $query = $DB->query("SELECT country_code, country_id, IF(country_code = '".$country_code."', '', '') AS sel FROM countries WHERE site = 'test' 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 is fine and outputs all the input fields I need.

 

The problem comes when I need to get the value of each field input field and pass it into a query.

 

The 'value' field of the inputs are numbers, such as 68, 70, 124, 108 etc so I can't get them in a for loop etc

 

What's the easiest way to get all of these input field values?

 

Can I use $_POST?

Link to comment
https://forums.phpfreaks.com/topic/219204-getting-dynamically-created-form-values/
Share on other sites

I don't see how your code is working. I see two issues:

 

1. According to the MySQL Manual, this code:

IF(country_code = '".$country_code."', '', '')

means that if country_code is equal to $country_code, then use '' (an empty string), otherwise use '' (again, an empty string). You might as well just use '' instead of the whole IF(...) construct.

 

2. In HTML, label IDs (or any other IDs for that matter) must be unique. While most mainstream browsers can tolerate invalid HTML to a certain extent, PHP does not.

 

That said, can you fix these two issues and post the complete <form> ... </form> code? Also have a look at How do I create arrays in a HTML <form>?

change form part to

<?php
$query = $DB->query("SELECT country_code, country_id, IF(country_code = '".$country_code."', '', '') AS sel FROM countries WHERE site = 'test' 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_data[' . $row['country_id'] . ']"  />' . '<br /><br />';
}
?>

and on submit page do

<?php
foreach ($_POST['country_data'] as $country_id => $value) {
    // do something
}
?>

Ok, this seems to work ok.

 

My only problem now is I need to not only insert the value in the text box but also the country_id

<input type="text" name="country_data[69]" id="country_data" style="width: 100%; height: 5%;">

<input type="text" name="country_data[89]" id="country_data" style="width: 100%; height: 5%;">

<input type="text" name="country_data[45]" id="country_data" style="width: 100%; height: 5%;">

 

So When the form is submitted, I also need to get the values of 69, 89, 45 etc

 

Is this possible?

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.