0

I have the ability to add custom fields in a project I am making. I have a page that has all the text inputs on it. Each custom field is named consecutively (field1, field2, field3) according to the order they were created. Since the user will have the ability to add as many as they want, how can I select each one so as to post their values to the database?

Hope this makes sense...

2
  • Where do you want to select them? What do you mean with that? Sounds like an array would be more appropriate. Commented Jan 2, 2011 at 22:36
  • I need to select each value from the input to add its data to a table Commented Jan 2, 2011 at 22:41

4 Answers 4

7

You should name the fields with array notation, as follows:

<input name='field[]' type='text' />
<input name='field[]' type='text' />

You can then retrieve the data from $_POST (or $_GET) as

$_POST['field'][0]
$_POST['field'][1]
Sign up to request clarification or add additional context in comments.

3 Comments

I couldn't do this because I don't know how many inputs will be used. It will change depending on how many the user adds. So somehow I need to dynamically get all the values from all fields added.
use foreach ($_POST['field'] as $index => $value) { echo $value; } to "dynamically" get the values :)
@codedude -- it doesnt MATTER How many fields they are. You name them ALL field[] .. its an array notation. It creates additional values in the $_POST['field'][n] array as needed. You can have 50000 values or 2, it doesn't matter.
1

try this:

for($i=1;$i<=3;$i++) {
  print ${'field' . $i} . "<br>";
}

4 Comments

Ugh, horrible. I don't know who thought variable variable names was a good idea.
@You: The same people that brought us magic_quotes. But joking aside, varvars are a useful syntax construct. Other scripting languages encourage heaps of eval() in lieu of that. (And you thought it was bad in PHP!)
i have mixed opinions on variable variables, i used to use them all the time in Perl, but shy from them these days, as it suggests there might be a better way of approaching the code - but in terms of the OP's question for 'variable variables', i thought it was as close to what he was expecting as possible :) @Erik's solution gets my 'refactor the approach' vote tho.
@mario, that's why I like Ruby and Python a bit more every day :)
0

Here's how you would collect them if they are passed in _GET/_POST

$i = 1;
$custom_fields = array();

while (!empty($_REQUEST["field$i"]) {
    $custom_fields[] = $_REQUEST["field$i"];
    $i++;
}

Comments

0

Assuming that each of these fields will be posted, you can use a simple while loop and check that the variable is set:

$i = 1;
while ( isset($_POST['field' . $i]) ) {
    // Do what you need to do to the variable here    

    // Whatever you do, do not forget this line
    $i += 1;
}

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.