PHP Form with multiple text fields for email addresses

Hello,
I have a site that I need to create a form for users to share the site with others.
So I have constructed a text field where once our clients pay for their class they are going to take, they can send the class to friends.

So far I have a form with 5 email address fields and a comments box. In PHP how can i action the form so that for each email address field filled out, the action page will send an email to each address with a link, the comments box contents and some more text about our website.

I know how to do a PHP mail form, just not sure on how to create code that will send to a different number of email addresses, depending on what the customer filled out.

here is the form so far.

<form action="" method="get"><table width="200" border="0" class="table1">
  <tr>
    <td>To:</td>
    <td><label for="emailaddress"></label>
      <input type="text" name="emailaddress" id="emailaddress" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="text" name="emailaddress2" id="emailaddress2" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="text" name="emailaddress3" id="emailaddress3" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="text" name="emailaddress4" id="emailaddress4" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="text" name="emailaddress5" id="emailaddress5" /></td>
  </tr>
  <tr>
    <td colspan="2"><label for="textarea"></label>
      <textarea name="textarea" id="textarea" cols="45" rows="5"></textarea></td>
    </tr>
  <tr>
    <td colspan="2"><input type="submit" name="send" id="send" value="Submit" /></td>
  </tr>
</table>
</form>

I don’t know if the following solution works with ‘get’, but it does with ‘post’. So if you want to use get, try it. If it doesn’t work, you might want to switch to post.
Give all email fields the name “emailaddress” that way in the PHP script $_POST[‘emailaddress’] will contain an array of the email addresses, and you can loop through the array with a normal foreach loop.

Hello,

this code should work for your current formular:

if (!empty($_GET)){
	$numEmail = 5;	
	
	for ($i = 1; $i < $numEmail; $i++){
		if ($i == 1)
			$to      = $_GET['emailaddress'];
		else
			$to      = $_GET['emailaddress'.$i];
			
		$subject = 'the subject';
		$message = $_GET['textarea'];
		$headers =
			'From: webmaster@example.com' . "\\r\
" .
			'Reply-To: webmaster@example.com' . "\\r\
" .
			'X-Mailer: PHP/' . phpversion();

		mail($to, $subject, $message, $headers);
	}
}

If you are generating new email fields with javascript and you want to keep formular in your current format, you could create a field with name, for example “numEmail” and always increment the value in the field by one after user adds a new field for email addres. The code I posted would have different second line:

$numEmail = $_GET['numEmail'];

Other solution would be to change your formular in this way:

<form action="" method="get"><table width="200" border="0" class="table1">
  <tr>
    <td>To:</td>
    <td><label for="emailaddress"></label>
      <input type="text" name="emailaddress[]" id="emailaddress" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="text" name="emailaddress[]" id="emailaddress2" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="text" name="emailaddress[]" id="emailaddress3" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="text" name="emailaddress[]" id="emailaddress4" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="text" name="emailaddress[]" id="emailaddress5" /></td>
  </tr>
  <tr>
    <td colspan="2"><label for="textarea"></label>
      <textarea name="textarea" id="textarea" cols="45" rows="5"></textarea></td>
    </tr>
  <tr>
    <td colspan="2"><input type="submit" name="send" id="send" value="Submit" /></td>
  </tr>
</table>
</form>

After submitting the formular you would get array which would look like this:

array(3) { ["emailaddress"]=> array(5) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" [4]=> string(1) "e" } ["textarea"]=> string(0) "" ["send"]=> string(6) "Submit" } 

The code which I posted above would look for this formular like this:

if (!empty($_GET)){
	foreach ($_GET['emailaddress'] as $v){
		$to = $v;
		$subject = 'the subject';
		$message = $v['textarea'];
		$headers =
			'From: webmaster@example.com' . "\\r\
" .
			'Reply-To: webmaster@example.com' . "\\r\
" .
			'X-Mailer: PHP/' . phpversion();

		mail($to, $subject, $message, $headers);
	}
}

Ok thanks.
Does this take into consideration if a user only types in one or two addresses, (leaving 3 boxes blank?) I guess the second one does as its using foreach. but the first one?

My advice is to use the second one. If you ever want to increase the number of email fields, or make a ‘add more email fields’ button, the array solution is flexible, and you won’t need to change the php script.

If you want to know how they work in certain situations, try them, and see what happens. :slight_smile: