This is a handy little tut, if you want to have the capability, of using just one input, to do something for each value in it.
First of all, we're going to use the function explode() to seperate our values. To begin, here's simple little script, that you could use to email people depending on emails submitted by someone, seperating them with line breaks.
PHP Code:
<?php
if($_POST['submit']){
$emails=$_POST['emails'];
$email=explode(chr(13), $emails);
foreach($email as $email){
mail($email, "You've been referred", "You have been referred by mr. blah, to do.....", "email@email.com");
}
}
else{
echo "<form action='". $PHP_SELF. "' method='post'>
Enter as many emails as you would like, with a line break between them:<br />
<textarea name='emails'></textarea><br />
<input type='submit' name='submit' value='Send'>
</form>";
}
?>
First of all, we see that if 'submit' has been posted, and then simplify the submitted emails into the variable $emails:
PHP Code:
if($_POST['submit']){
$emails=$_POST['emails'];
Next, we define a new variable $email, to be exploded into an array of all the emails someone has submitted, seperating them with line breaks:
PHP Code:
$email=explode(chr(13), $emails);
With explode(), you first string the seperating charactor or phrase, then the origional value to seperate ($emails in this case), then if you want, you can string a limit on how many you will allow, however i didn't add that for this tutorial.
Now that we have all the emails submitted into an array, we'll use forreach(){} to send an email to each of those emails individually:
PHP Code:
foreach($email as $email){
mail($email, "You've been referred", "You have been referred by mr. blah, to do.....", "email@email.com");
}
Of course, you could make it a much or interesting message (lol), but this is just an example. Also, remember to change
email@email.com to your email address :P
Then, we just have the else{} part of the script, which just echos out the form for a user to submit their emails.
PHP Code:
else{
echo "<form action='". $PHP_SELF. "' method='post'>
Enter as many emails as you would like, with a line break between them:<br />
<textarea name='emails'></textarea><br />
<input type='submit' name='submit' value='Send'>
</form>";
}
That's just a simple tut, but i like it, because i find that explode() is quite a usefull little function. It can be used for several things, and you don't always need to use it in an array. Say you had a variable which you only wanted to keep the first sentance of, you could use this:
PHP Code:
$sentance="Blah blah blah blah. More blah more blah more blah.";
$newsentance=explode('.', $sentance);
echo $newsentance[0];
And that will just echo the first sentance, then if you changed the 0, every time it increases, it would display the next sentace
I hope some of you find this usefull