Reputation: 318
I have a form. It's a "friend referral" form so they can refer their friends and get rewarded. The form let's them "Add a friend" so they click a button and three new input fields show up - asking them for their name, email, and mobile. Each time a new input field is generated, they get a new 'name' attribute. The first batch of input fields are all 'name_1', 'email_1', 'mobile_1' and if they press the button again the number goes up to 'name_2', 'email_2', and 'mobile_2'. This is my code when the form is submitted.
if(isset($_POST['refer_send'])) {
$friend_no = $_POST['counter_friends'];
$fname = $_POST['fname'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
This code above is not duplicated - it's the person who's filling out the form's details. 'friend_no' counts the number of times the person has 'added a friend'.
for ($x = 1; $x <= $friend_no; $x++) {
$friend_title = "Friend $x";
$friend_name = $_POST["fname_$x"];
$friend_email = $_POST["email_$x"];
$friend_mobile = $_POST["mobile_$x"];
}
I now want to send all this in an email. How can I add all of this in one variable? I want to access $friend_title and all the others outside of the for loop so I can add them to $msg that I will send using mail().
Here is the rest of my code.
// Send email
$email = "[email protected]";
// subject
$sub = "Test";
// the message
$msg = "
<strong>Refer a Friend</strong>
<br><br>
<strong>Full Name:</strong> $fname
<br>
<strong>Email: </strong> $email
<br>
<strong>Mobile: </strong> $mobile
<br>
This person referred $friend_no friends. Information below.
<hr>
// **HERE I WANT TO SHOW ALL THE FRIENDS INFORMATION**
";
// send email
mail($email, $sub,$msg);
Upvotes: 1
Views: 53
Reputation: 12452
A solution with the code you've given us, you can create a string of the friends data and append them to your mail.
And you should escape variables in strings. Like I did in the for
loop with $x
, or like I did in the $msg
string with {}
.
// Send email
$email = "[email protected]";
// subject
$sub = "Test";
// the message
$msg = "
<strong>Refer a Friend</strong>
<br><br>
<strong>Full Name:</strong> {$fname}
<br>
<strong>Email: </strong> {$email}
<br>
<strong>Mobile: </strong> {$mobile}
<br>
This person referred {$friend_no} friends. Information below.
<hr>";
// add friends to message
for ($x = 1; $x <= $friend_no; $x++) {
$msg .= "Friend " . $x . "<br />";
$msg .= "- Name: " . $_POST["fname_" . $x] . "<br>";
$msg .= "- EMail: " . $_POST["email_" . $x] . "<br>";
$msg .= "- Mobile: " . $_POST["mobile_" . $x] . "<br><br>";
}
// send email
mail($email, $sub, $msg);
Upvotes: 1