Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4624862
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:11:24+00:00 2026-05-22T03:11:24+00:00

I am using a PHP script to generate an email based on the information

  • 0

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\">";
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:11:25+00:00Added an answer on May 22, 2026 at 3:11 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that will generate a report using PHPExcel from data
I have a simple php script on a server that's using fsockopen to connect
I'm using jQuery to post a form to a php file, simple script to
In my web application I render pages using PHP script, and then generate static
Is there a way to generate an HTML file using a PHP script which
I have written a script for my clients to generate a newsletter form, embed
I have to compare 2 xml files and generate a patch using php and
I have a simple PHP script using imagecreatefromjpeg to create a thumbnail version of
I have a PHP script that generates an RSS feed using some external data
I installed wkhtmltopdf using sudo apt-get install wkhtmltopdf I have a php script that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.