Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>";
}
share|improve this question
add comment (requires an account with 50 reputation)

3 Answers

up vote 0 down vote accepted

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'])) {
    /* ... */
}
share|improve this answer
Perfect, just what I needed. – enrique.ortsa Aug 2 '12 at 15:39
add comment (requires an account with 50 reputation)

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);

share|improve this answer
That worked like a charm, thanks, but I had to write it likeSherbrow explained in the answer below, $empleado = $POST['classEmpleado']; – enrique.ortsa Aug 2 '12 at 15:38
add comment (requires an account with 50 reputation)

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 />';
}
share|improve this answer
This will be the structure of $_POST array : Array ( [classEmpleado] => Array ( [0] => Some text 0 [1] => Some text 1 [2] => Some text 2 ) ) – Bilel Chihi Aug 2 '12 at 14:24
Thanks for your response, I'll try this in another web form that I intend on making, appreciated! – enrique.ortsa Aug 2 '12 at 15:39
You shouldn't put count() inside the loop condition, it will get executed EVERY iteration of the loop. – Dunhamzzz Aug 2 '12 at 15:56
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.