I have a table that allows adding, editing and deleting rows, I created a 'foreach' for the different arrays.
Now when submitting the form the data is printed on the screen, however to the email it only sends the last data of the array.

Thank you!

<?php

foreach ($_POST['produto'] as $item){echo 'Produto:' . $item . '<br>';}
foreach ($_POST['referencia'] as $itemr){echo 'Referência:' . $itemr . '<br>';}
foreach ($_POST['quantidade'] as $itemq){echo 'Quantidade:' . $itemq . '<br>';}
foreach ($_POST['preco'] as $itemp){echo 'Preco:' . $itemp . '<br>';}

$mensagem_form = $_POST['obs'];

$to = "email@email.pt";
$remetente = "email@email.pt";



$boundary = date("d-m-Y");
$headers.= "Nova Encomenda - ";
$headers.= "$boundary\n"; 



$corpo_mensagem = "

ENCOMENDA
Produto: $item 
Referência: $itemr
Quantidade: $itemq
Preço: $itemp €


OBSERVAÇÕES
Mensagem: $mensagem_form
";


$mensagem = "--$boundary\n"; 
$mensagem.= "Content-Transfer-Encoding: 8bits\n"; 
$mensagem.= "Content-Type: text/html; charset=\"utf-8\"\n\n";
$mensagem.= "$corpo_mensagem\n";



if(mail($to, $headers, $corpo_mensagem)){
 echo "<br><br><center><b><font color='green'>Mensagem enviada com sucesso!<br><br><a href='http://conceitosdiferentes.org/batist/encomendas.html'>Voltar</a>";
} else{
 echo "<br><br><center><b><font color='red'>Ocorreu um erro ao enviar a mensagem!<br><br><a href='http://conceitosdiferentes.org/batist/encomendas.html'>Voltar</a>";
}
?>

    Welcome to the forums. Note that I edited your post to use [code]...[/code] tags around your code, as that formats code blocks much better than the "`" character (which is really only good for in-line bits of code within a normal paragraph).

    Now I'll actually take a look at your question.... 🙂

      The quickest solution may be to have each of those foreach() loops also write to your $corpo_mensagem, something like

      $corpo_mensagem = "
      
      ENCOMENDA
      ";
      
      foreach ($_POST['produto'] as $item){
        echo 'Produto:' . $item . '<br>';
        $corpo_mesagem .= "Produto: $item\n";
      }
      // same idea for each of the other 3 foreach()'s
      // Then later on:
      $corpo_mensagem .= "
      OBSERVAÇÕES
      Mensagem: $mensagem_form
      ";
      

      However, if things are not organized the way you want, it may make more sense to use those foreach() loops to populate some sort of mutli-dimension array, which you could then iterate through as needed to output your HTML and to populate the email text?

        Write a Reply...