Solved

Tell to friend php form

Posted on 2010-09-03
13
1,114 Views
Last Modified: 2012-05-10
I want make little tell to friend form and there is one field where all friends email address goes separate dot, commas or point. But I don't figure out how it works :)

Here is my form without action (send page) because I haven't do it yet.


<table border="0" cellpadding="10" cellspacing="0">
<tr>
<td width="397"> *Your Friend's email address:</td>
<td width="301">&nbsp;</td>
</tr>
<tr>
  <td><input size="30" name="friends_address" maxlength="45"></td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>*Sender emailtd>
  <td>&nbsp;</td>
</tr>
<tr>
  <td><input size="30" name="sender_email" maxlength="45"></td>
  <td>&nbsp;</td>
</tr>
<tr>
<td> *Nimesi:</td>
<td rowspan="5" align="right">&nbsp;</td>
</tr>
<tr>
<td><input size="30" name="name" maxlength="45"></td>
</tr>

<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>Viestisi</td>
</tr>
<tr>
<td><label>
  <textarea name="message" id="viestisi" cols="60" rows="5"></textarea>
</label></td>
</tr>
<tr>
<td>
<p align="left"> Voi l&auml;hett&auml;&auml; suosituksen my&ouml;s usealle vastaanottajalle. Erota osoitteet pilkulla ja v&auml;lily&ouml;nill&auml; toisistaan. <br>Sähköpostiosoitteesi ja nimesi näytetään vastaanottajalle.<br>
<br>
*Tähdellä merkityt ovat pakollisia
</td>
<td align="right"><input onClick="validate();" type="button" value="Suosittele"></td>
</tr>
</table>
</center>
</div>
</form>
</td>
</tr> 
<tr valign="top"> 
<td valign="middle" align="center">&nbsp; 

</td>
</tr> 
</table>

Open in new window

0
Comment
Question by:mattimeikalainen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33595652
You can split it with explode,

ref. http://php.net/manual/en/function.explode.php
0
 
LVL 2

Accepted Solution

by:
Tiller79188231 earned 450 total points
ID: 33595962
you have no opening form tag.
are you wanting to split the emails in php?
if so it would be done like this

$emails = explode(",", $stringOfEmails);
$emails would now be an array of the emails entered
0
 
LVL 8

Expert Comment

by:kumaranmca
ID: 33596250
there two functions are available in php

1. explode
2. split

to split the string to array and make it an array. To get the user entered email ids. Please refer the below url.

http://www.plus2net.com/php_tutorial/tell_friend.php
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33596259
When I want to add a "send this page to a friend" link to a web page, I let the browser handle it.  Almost every PHP installation populates these fields, and almost every browser obeys the "mailto" link.  Pretty easy.  Plus, you do not have the issue of client reluctance since they are not exposing their email address or their friends' email address(es) to the risk of getting captured in a spam machine.


<?php // RAY_email_page_to_friend.php
error_reporting(E_ALL);

// DEMONSTRATE HOW TO ADD A "SEND THIS PAGE" LINK

$y = $_SERVER["HTTP_HOST"] ;
$z = urlencode($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);

echo "<a href=\"mailto:?subject=$y&body=$z\">send this page to a friend</a>";

Open in new window

0
 

Author Comment

by:mattimeikalainen
ID: 33608832
I try next send code, but I don't figure out how this split or explode works.  Tiller79188231 and kumaranmca your looks right to me but I don't understand how it has to be in my code :-(
<?php


if(count($_POST)) {

foreach(array('friendmail1','friendmail2','friendmail3','email','name') as $key) $_POST[$key] = strip_tags($_POST[$key]);
if(!is_secure($_POST)) { die("Asta la vista Hacker");}

$emailto = "mail@mail.com"; 
$esubject = "Viesti on lähetetty"; 
$emailtext = "
$_POST[name] Kerro kaveria lomaketta on käytetty. Käyttäjän sähköpostiosoite on $_POST[email]
Sivua suositeltiin seuraaville henkilöille:

$_POST[friendmail1]
$_POST[friendmail2]
$_POST[friendmail3]

";

@mail("$emailto", $esubject, $emailtext, "From: $_POST[email]");

$thankyoupage = "kiitos.php"; 
$tsubject = "Suositus ystävältäsi $_POST[name]";
$ttext = "
Hei,
$_POST[name] On lähettänyt tämän viestin sinulle ja hän arvelee sinun pitävän alla olevasta sivusta.

http://www.xxx.com/

";

@mail("$_POST[friendmail1],$_POST[friendmail2],$_POST[friendmail3]", $tsubject, $ttext, "FROM: $_POST[email]");

header("Location: $thankyoupage");
exit;

}



function is_secure($ar) {
$reg = "/(Content-Type|Bcc|MIME-Version|Content-Transfer-Encoding)/i";
if(!is_array($ar)) { return preg_match($reg,$ar);}
$incoming = array_values_recursive($ar);
foreach($incoming as $k=>$v) if(preg_match($reg,$v)) return false;
return true;
}

function array_values_recursive($array) {
$arrayValues = array();
foreach ($array as $key=>$value) {
if (is_scalar($value) || is_resource($value)) {
$arrayValues[] = $value;
$arrayValues[] = $key;
}
elseif (is_array($value)) {
$arrayValues[] = $key;
$arrayValues = array_merge($arrayValues, array_values_recursive($value));
}
}
return $arrayValues;
}
?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33610468
From that code snippet, it looks like you need a learning resource to get some foundation in PHP.  Consider getting this book:
http://www.sitepoint.com/books/phpmysql4/

The Sitepoint book will not make you a pro, but if you give yourself a month to read it and work through the examples, you will be way ahead.

Here are the links that tell how the PHP functions split() and explode() work.  Note the large red warning box on split().
http://us2.php.net/manual/en/function.split.php
http://us2.php.net/manual/en/function.explode.php

The standard "form-to-email" script is pretty well understood in the industry today.  Here is a simple teaching example.  Looks like you would want to use information from the POST form to populate the $to array.  To avoid becoming an open relay for spam, you need to make sure you have clean strings.  Note that there will be no commas or semi-colons permitted in the external data.

HTH, ~Ray
<?php // RAY_form_to_email.php
error_reporting(E_ALL);

// SEND MAIL FROM A FORM

// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";

// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE
    $content    = '';
    $content   .= "You have a New Query From $name \n\n";
    $content   .= "Tel No: $telephone\n";
    $content   .= "Email: $email\n";

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
} // END OF PHP - PUT UP THE FORM
?>
<form method="post">
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>

Open in new window

0
 

Author Comment

by:mattimeikalainen
ID: 33612271
Yes I am not so familiar with php, but can you help me. I can't figure out this :( That eplode system is  the answer but how i get those from post method with the form field friends_address,
something like:  
$to[]  = "explode(",", $friends_address);";

??
0
 
LVL 2

Expert Comment

by:Tiller79188231
ID: 33617163
foreach(array('friendmail1','friendmail2','friendmail3','email','name') as $key)

why is the a foreach??.. the form can only be submitted once...

HOW are the email addresses your wanting to send mail to inputted into the form?.. same text box?.. different text box??
0
 

Author Comment

by:mattimeikalainen
ID: 33617813
First I have three different email filelds friendmail1, friendmail2 and friendmail3. But is it possible to have one email field where can write all email addresses separated with , ? mail1@hotmail.com, mail2@hotmail.com, mail3@hotmail.com and so on one input field...
Don't look at mine code, that is very wrong. I thinks that $emails = explode(",", $stringOfEmails); is right but I don't figure out how it works...

My form  are only next fields: sender address field, friends address field (where goes all email addresses separated with ,)  and message field. The goes also somekind of message "your friends thinks that you like this site" or something...
0
 
LVL 2

Expert Comment

by:Tiller79188231
ID: 33617893
So you would get the information from the text field

$emails = explode(",", $_POST['Field_with_emails_seperated_by_comma'];

foreach($emails as $sendToEmail){


//code to send email here


}
0
 

Author Comment

by:mattimeikalainen
ID: 33624144
Hello Tiller79188231 and thank you of your answer :-)

Now I get an error Parse error: syntax error, unexpected ';' in /var/www/customers/mlifefi/public_html/kerrokaverille/2.php on line 14

Here is action page code: I find another tell a friend code which i test. I just figure out that form have to work also only one email address. One or more email address.

<?php
$fromName=$_REQUEST["fromName"];
$fromEmail=$_REQUEST["fromEmail"];
$toName=$_REQUEST["toName"];
$emails = explode(",", $_POST['toEmail'];
foreach($emails as $sendToEmail){
if ($fromEmail) { $ref=getenv('HTTP_REFERER');
if ($fromName=='')
$subject= "Your friend has referred you this link from www.sitename.com";
else $subject= "$fromName has referred you this link from www.sitename.com";
$message = " Dear $toName, Your friend $fromName has referred this link to you. Please visit this link by clicking $ref ";
$from = "From: $fromEmail\r\n"; mail($toEmail, $subject, $message, $from); header ("location:".$ref); }
?>
0
 
LVL 2

Expert Comment

by:Tiller79188231
ID: 33626433
I would try and layout your code a little easier to read for yourself...

$emails = explode(",", $_POST['toEmail']; should be $emails = explode(",", $_POST['toEmail']);
and you are missing a closing bracket at the end to close out your foreach statement

here is the fixed code

<?php
$fromName=$_REQUEST["fromName"];
$fromEmail=$_REQUEST["fromEmail"];
$toName=$_REQUEST["toName"];

$emails = explode(",", $_POST['toEmail']);


foreach($emails as $sendToEmail){
      if ($fromEmail) {
            $ref=getenv('HTTP_REFERER');
            if ($fromName=='')
                  $subject= "Your friend has referred you this link from www.sitename.com";
            else
                  $subject= "$fromName has referred you this link from www.sitename.com";
                  
            $message = " Dear $toName, Your friend $fromName has referred this link to you. Please visit this link by clicking $ref ";
            $from = "From: $fromEmail\r\n";
            mail($toEmail, $subject, $message, $from);
            header ("location:".$ref);
      }
}
?>
0
 
LVL 2

Expert Comment

by:Tiller79188231
ID: 33626450
Oh and also, your foreach statement will only run for one email.. because the last line.. "header("Location:".$ref);".. will send the browser to another page and stop the script from running
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question