Hi, I have a simple for with various text areas to fill in. 3 of them are arrays. cod[], descri[] and qty[] I'm emailing this form to my e-mail, and all the code works fine except the array fields, that only show the 1st element of the array. To give you a prespective, this is the code where i pull the array: -- $cod = $_POST['cod']; foreach($cod as $c) { if( isset($c) && $c<>'') { $EmailBody .="Codigo: $c\n"; } } $cod = $_POST['descri']; foreach($descri as $d) { if( isset($d) && $d<>'') { $EmailBody .="Descricao: $d\n"; } } $cod = $_POST['qty']; foreach($qty as $q) { if( isset($q) && $q<>'') { $EmailBody .="Numero de amostras: $q\n"; } } -- The above code is giving me the first element of the array. Is there something wrong with the code? Can you help please? Thank you. António
Hi, please try this code: $cod = $_POST['cod[]']; foreach($cod as $c){ $c=trim($c); if(!empty($c)) $EmailBody .= "Codigo: {$c}\n"; } PHP: Regards
I think you should check this lines: $cod = $_POST['descri']; foreach($descri as $d){... PHP: may be the variable should be the same as the one in the foreach.
Those solutions are really bad - it runs on every index, even though you just need the first. That's waste of processing power (and in the end results in slower parsing of your script). Just put break 1; in the bottom of your foreach constructor. That will end the loop after first run. $cod = $_POST['cod[]']; foreach($cod as $c){ $c=trim($c); $EmailBody .= "Codigo: {$c}\n"; break 1; } PHP:
If I understood right, problem is, that it grabs first only element. If we need only the first element, it is simply $_POST['cod'][0] Otherwise $EmailBody .= implode(' ',$_POST['cod']); PHP: Regards
To make and array with the form you must give the name like this: name="thisarray[]" Not like this: name="thisarray" Hope this help you