Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using a PHP script to generate an email based on the information from a form. The form has a variable number of rows.

I have converted the names of the inputs in each row in the form to an array, by adding [] after the name, so that the data in all of the rows is available for generating the email.

However, what I don't know how to do is how to construct the PHP so that it can generate an email with just the right number of rows in the email.

At the moment I have just set the PHP to read the first 5 items in the array for each input, and construct the body of the email using these. The problem with this approach is that if the user adds more than 5 rows data will be lost, and if there is less than 5 rows, there will be unnecessary text in the email for 'name, email, telephone'.

I wonder if there is a way of getting the PHP to read the array for any number of rows, and generate an email with just the correct number of rows? I have included the PHP as it stands below.

Thanks,

Nick

<?php

$EmailFrom = "";
$EmailTo = "";
$Subject = "";
$Name = Trim(stripslashes($_POST['name'][0])); 
$Email = Trim(stripslashes($_POST['email'][0])); 
$Telephone = Trim(stripslashes($_POST['telephone'][0]));
$Name2 = Trim(stripslashes($_POST['name'][1])); 
$Email2 = Trim(stripslashes($_POST['email'][1])); 
$Telephone2 = Trim(stripslashes($_POST['telephone'][1]));
$Name3 = Trim(stripslashes($_POST['name'][1])); 
$Email3 = Trim(stripslashes($_POST['email'][1])); 
$Telephone3 = Trim(stripslashes($_POST['telephone'][2])); 
$Name4 = Trim(stripslashes($_POST['name'][1])); 
$Email4 = Trim(stripslashes($_POST['email'][1])); 
$Telephone4 = Trim(stripslashes($_POST['telephone'][3])); 
$Name5 = Trim(stripslashes($_POST['name'][1])); 
$Email5 = Trim(stripslashes($_POST['email'][1])); 
$Telephone5 = Trim(stripslashes($_POST['telephone'][4])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "New bookings have been made for the Ajahn Amaro Retreat as follows:";
$Body .= "\n";
$Body .= "\n";
$Body .= "name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "\n";
$Body .= "email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "\n";
$Body .= "\n";
$Body .= "name: ";
$Body .= $Name2;
$Body .= "\n";
$Body .= "\n";
$Body .= "email: ";
$Body .= $Email2;
$Body .= "\n";
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $Telephone2;
$Body .= "\n";
$Body .= "\n";
$Body .= "\n";
$Body .= "name: ";
$Body .= $Name3;
$Body .= "\n";
$Body .= "\n";
$Body .= "email: ";
$Body .= $Email3;
$Body .= "\n";
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $Telephone3;
$Body .= "\n";
$Body .= "\n";
$Body .= "\n";
$Body .= "name: ";
$Body .= $Name4;
$Body .= "\n";
$Body .= "\n";
$Body .= "email: ";
$Body .= $Email4;
$Body .= "\n";
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $Telephone4;
$Body .= "\n";
$Body .= "\n";
$Body .= "\n";
$Body .= "name: ";
$Body .= $Name5;
$Body .= "\n";
$Body .= "\n";
$Body .= "email: ";
$Body .= $Email5;
$Body .= "\n";
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $Telephone5;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=payment.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Instead of assigning each one to a unique variable, just put them in an array.

$body = '';
$row_count = count($_POST['name']);

for($i = 0; $i < $row_count; $i++)
{
  // variable sanitation...
  $name = trim(stripslashes($_POST['name'][$i]));
  $email = trim(stripslashes($_POST['email'][$i]));
  $telephone = trim(stripslashes($_POST['telephone'][$i]));

  // this assumes name, email, and telephone are required & present in each element
  // otherwise you will have spurious line breaks. 
  $body .= $name . "\n\n" . $email . "\n\n" . $telephone . "\n\n";
}

// send email 
$success = mail($emailTo, $subject, $body, "From: <$EmailFrom>");

Also on a purely stylistic note, your variables should begin with lower-case letters.

share|improve this answer
1  
Use var_dump() and see what isn't set as you think. I changed the case on $body, $emailTo, and $subject. If you copy & pasted, this could be a potential source of error. –  KyleWpppd May 15 '11 at 19:57
    
@KyleWpppd Thanks Kyle, that looks like it should do what I am looking for, but for some reason it isn't quite working. I tried it with a form with 2 rows and got an email that just contained '0' –  Nick May 15 '11 at 19:57
    
@KyleWpppd I was just editing my comment when your comment came through. Yes, I noticed you changed the case, so I have accounted for that. The issue seems to be with the body variable. All the other variables return the last value correctly, but var_dump($body) just returns string(0) "" –  Nick May 15 '11 at 20:08
    
@KyleWpppd I noticed the + sign before = sign and removed this, and now the email contains the last row of the form only, which is progress, but... –  Nick May 15 '11 at 20:12
1  
Sorry, I gave you the wrong concatenation operator. Change the += to .= and it should work. I'm updating my answer. Using += as I did makes the strings evaluate to an integer which will not work. –  KyleWpppd May 15 '11 at 20:27

Do something like this, iterating over one of the arrays of data:

foreach($_POST['name'] as $i => $name){
   echo $name;
   echo $_POST['email'][$i];
   echo $_POST['telephone'][$i];
}

Except instead of printing the data, add it to the string that will be your email's body.

share|improve this answer
    
Beat me too it, good answer –  Jake N May 15 '11 at 16:52
    
I do not like this use of the foreach loop. Also, this throws a nasty error if the array is empty. So you need to check for it before starting the foreach loop. –  KyleWpppd May 15 '11 at 17:05

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.