Question

Hi I have question about HTML form array and PHP. For example I have name and email but ask 6 times and I want to send this mail. What I should edit to work? thank you!

HTML:

   <form  method="Post" action="send.php" onSubmit="return validate();">
    <?php
     $amount=6; //amount shows the number of data I want to repeat
     for( $i = 0; $i < $amount; $i++ ) {
        ?>
        <b>Data <?php echo $i+1 ?>º</b>
          <input type="edit" name="Name[]" size="40">
          <input type="edit" name="email[]" size="40">
          <br>
    <?php } ?>
          <input type="submit" name="Submit" value="send 6 data mail">
   </form>

send.php:

<?php
       require('phpmailer/class.phpmailer.php');
       $name= $_POST['name[]']; 
       $email= $_POST['email[]']; 
       $mail = new PHPMailer();
       ...
       $mail->Body = ' Data<br> '
    <?php
     $amount=6; //amount shows the number of data I want to repeat
     for( $i = 0; $i < $amount; $i++ ) {
        ?> '
       name: '.$name[$i].' email: '.$email[$i];
       ...
       $mail->Send();
?>

should send:

Data
name: nameinput1 email: emailinput1
name: nameinput2 email: emailinput2
name: nameinput3 email: emailinput3
name: nameinput4 email: emailinput4
name: nameinput5 email: emailinput5
name: nameinput6 email: emailinput6
Was it helpful?

Solution

You can use following;

 <?php
    require('phpmailer/class.phpmailer.php');
    $name= $_POST['name']; 
    $email= $_POST['email'];

    $mail = new PHPMailer();
    ...
    $mail->Body = ' Data<br> ';
    $amount=count($name); //amount shows the number of data I want to repeat
    for( $i = 0; $i < $amount; $i++ ) {
        $mail->Body .= 'name: '.$name[$i].' email: '.$email[$i];
    } 
    ...
    $mail->Send();
?>

OTHER TIPS

You can try below:

$mail->Body = ' Data<br> ';
 $amount=6; //amount shows the number of data I want to repeat
 for( $i = 0; $i < $amount; $i++ ) {
   $mail->Body .= 'name: '.$name[$i].' email: '.$email[$i]."<br>";
 }
   ...
$mail->Send();
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top