terrid25 Posted November 19, 2010 Share Posted November 19, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/219204-getting-dynamically-created-form-values/ Share on other sites More sharing options...
Solaris Posted November 19, 2010 Share Posted November 19, 2010 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>? Quote Link to comment https://forums.phpfreaks.com/topic/219204-getting-dynamically-created-form-values/#findComment-1136747 Share on other sites More sharing options...
sasa Posted November 19, 2010 Share Posted November 19, 2010 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219204-getting-dynamically-created-form-values/#findComment-1136796 Share on other sites More sharing options...
terrid25 Posted November 23, 2010 Author Share Posted November 23, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/219204-getting-dynamically-created-form-values/#findComment-1138458 Share on other sites More sharing options...
sasa Posted November 24, 2010 Share Posted November 24, 2010 <?php foreach ($_POST['country_data'] as $country_id => $value) { // do something echo 'country_id is ' . $country_id . ' and value of text input field is ' . $value . "<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219204-getting-dynamically-created-form-values/#findComment-1138841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.