What does the script output as it is?
Personally, since you have a $counter variable I would do the following to retrieve the data that was POSTed to the page:
if (isset($_POST["submit"])) {
$count = $_POST['counter'];
$items = array(); //each element has another array inside it
for ($i=0; $i<$count; $i++){
$items[$i] = array('r'=>'', 'q'=>0, 'd'=>'', e=>''); //e for errors
//(expanded for clarity)
$items[$i]['r'] = $_POST['reference' . $i+1];
//$i+1 because you started at "reference1" and arrays start at 0
$items[$i]['q'] = $_POST['quantity' . $i+1];
$items[$i]['d'] = $_POST['details' . $i+1];
//$items[$i]['e'] = validItem($items[$i]);
} //I don't think I'm missing anything there...
}
Now that's pseudo because I haven't tested it or anything but you get the idea.
Your validItem() function could then look like this:
function validItem($item){
$errors = '';
if ($item['d'] /*is invalid*/){
$errors .= 'Description invalid. ';
}
if ($item['q'] /*is invalid*/){
$errors .= 'Quantity invalid. ';
}
//etc.
return $errors;
}
You can then print this to the screen like so: (maybe only do this if there are no errors)
<form id="purchaseOrder" name="purchaseOrder" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<? php
foreach ($items as $key=>$item){
echo '<div id="test" style="width: 650px;">';
echo '<label for="reference' . $key+1 . '" id="reference' . $key+1 . 'lbl" name="reference' . $key+1 . 'lbl">Reference</label>';
echo '<input id="reference' . $key+1 . '" name="reference' . $key+1 . '" type="text" value="'. $item['r'] .'" />';
echo '<label for="quantity' . $key+1 . '" id="quantity' . $key+1 . 'lbl" name="quantity' . $key+1 . 'lbl">Quantity</label>';
echo '<input id="quantity' . $key+1 . '" name="quantity' . $key+1 . '" type="text" value="'. $item['q'] .'" />';
echo '<label for="details' . $key+1 . '" id="details' . $key+1 . 'lbl" name="details' . $key+1 . 'lbl">Details</label>';
echo '<input id="details' . $key+1 . '" name="details' . $key+1 . '" type="text" value="'. $item['d'] .'" />';
if ($item['e'] != ''){ //errors are present for this item
echo '';
echo '<span id="bigAndRed">There is an error with this item: ' . $item['e'] . '</span>';
}
echo '</div>';
}
?>
<input name="counter" id="counter" type="text" value="1" />
<input name="submit" id="submit" type="submit" />
</form>
Hope this helps. If you feel like adopting this method you can PM me and I'll help you implement it if needed.