0

I have a dynamic web form that creates text after the user tells it how many he wants, what I want to do, is to get the info of those text fields into the next form, I've read a question that is
pretty much what I want to do;
But I haven't had any luck so far;

for ($i = 1; $i <= $quantity; $i++) {
echo "<input type='text' class='text' name='classEmpleado[]' id='empleado$i' />";}

When I try to retrieve them I use this;

$empleado[] = $_POST['classEmpleado[]'];
$i = 0;    
for ($i = 1; $i <= $quantity; $i++) {
echo "$empleado[$i]<BR><BR>";
}

But I get the error Undefined index: classEmpleado[] What am I doing wrong?

ANSWERED! For anyone looking for the same thing, look at the response of Sherbrow, And you would just have to edit the loop to this

$empleado[] = $_POST['classEmpleado[]'];
$i = 0;    
for ($i = 0; $i < $quantity; $i++) {
echo "$empleado[$i]<BR><BR>";
}

3 Answers 3

0

If $empleado is not previously declared or just empty, you are looking for that

$empleado = $_POST['classEmpleado']

But if $empleado is an array, and contains data, you may want to merge everything in one only array

$empleado = array_merge($empleado, $_POST['classEmpleado']);

Whichever way you choose, there should be a check to be certain that $_POST['classEmpleado'] is defined and is an array. Something like :

if(isset($_POST['classEmpleado']) && is_array($_POST['classEmpleado'])) {
    /* ... */
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try $empleado[] = $_POST['classEmpleado'];

When you put name[] at the end of the fieldname, it will be passed to PHP as an array in $_POST with the key of name like so: $_POST['name'] = array(1,2,3,4);

1 Comment

That worked like a charm, thanks, but I had to write it likeSherbrow explained in the answer below, $empleado = $POST['classEmpleado'];
-1

This statement here is wrong ($empleado[] = $_POST['classEmpleado[]'])

If you want to access $_POST the input field named classEmpleado[], you have to do so :

for($i=0; $i<count($_POST['classEmpleado']); $i++)
{
    echo $_POST['classEmpleado'][$i] . '<br /><br />';
}

3 Comments

This will be the structure of $_POST array : Array ( [classEmpleado] => Array ( [0] => Some text 0 [1] => Some text 1 [2] => Some text 2 ) )
Thanks for your response, I'll try this in another web form that I intend on making, appreciated!
You shouldn't put count() inside the loop condition, it will get executed EVERY iteration of the loop.

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.