Presto-X Posted October 13, 2009 Share Posted October 13, 2009 Hello everyone, I am working on a tell a friend script for my website, and I am running in to a bit of a problem, I want to send a message to each friend the visiting user adds (I’m using jQuery to add more than one friend field) my field names are fname1 femail1 the 1’s will increase with each set the visiting user adds. If I use this code it display the content and loops just fine: foreach($_POST as $key => $value){ if(substr($key, 0, 5) == "fname"): echo "Name: $value<br />"; endif; if(substr($key, 0, 6) == "femail"): echo "Email: $value<br /><hr />"; endif; } But if I replace this with my basic PHP email code the name and email no longer work. foreach($_POST as $key => $value){ if(substr($key, 0, 5) == "fname"): $friends_name = $value; endif; if(substr($key, 0, 6) == "femail"): $email = $value; endif; $subject = $params->get('subject'); $emailbody = '<html><head><title>'.$subject.'</title><style type="text/css">body, html{font-family:Arial, Helvetica, sans-serif;color:#666666;font-size:14px;}</style></head><body>'; $emailbody .= $message; $emailbody .= '</body></html>'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: '.$params->get('fromemail').' <'.$params->get('fromemail').'>' . "\r\n"; mail($email, $subject, $emailbody, $headers); } I was thinking this was going to very simple loop each name and email address and send an email, but it turns out I am not doing something right. Quote Link to comment Share on other sites More sharing options...
chaiwei Posted October 13, 2009 Share Posted October 13, 2009 before this mail($email, $subject, $emailbody, $headers); insert this echo $email,' - ',$subject,' - ',$emailbody,' - ', $headers; Quote Link to comment Share on other sites More sharing options...
pastcow Posted October 13, 2009 Share Posted October 13, 2009 A much easier way would be to do this.. <input name="email[]"/> <input name="email[]"/> <input name="email[]"/> And then you can use $_POST['email'][0], $_POST['email'][1] etc... from within your PHP script Quote Link to comment Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 A much easier way would be to do this.. <input name="email[]"/> <input name="email[]"/> <input name="email[]"/> And then you can use $_POST['email'][0], $_POST['email'][1] etc... from within your PHP script This is my code: $i = 0; foreach($_POST as $key => $value): $friends_name = $_POST['fname'][$i]; $friends_email = $_POST['femail'][$i]; Thanks pastcow, this seems to be mostly working now, let say I enter in 3 friends names (Joe, Mike, and Emily) it submits 3 emails just fine but it sends all 3 of them to Joe with Joe's name in each one? I was thinking that I may need to clean the variable before the next loop so I try to use unset('my_variable') with out any luck??? This is my code: $i = 0; foreach($_POST as $key => $value): $friends_name = $_POST['fname'][$i]; $friends_email = $_POST['femail'][$i]; $subject = $params->get('subject'); $emailbody = '<html><head><title>'.$subject.'</title><style type="text/css">body, html{font-family:Arial, Helvetica, sans-serif;color:#666666;font-size:14px;}</style></head><body>'; $emailbody .= $message; $emailbody .= '</body></html>'; mail($friends_email, $subject, $emailbody, $headers); $i++; endforeach; unset($friends_name); unset($friends_email); Quote Link to comment Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 Does something like this not do the job? foreach($_POST['femail'] as $key => $value): $friends_name = $_POST['fname'][$key]; $friends_email = $_POST['femail'][$key]; $subject = $params->get('subject'); $emailbody = '<html><head><title>'.$subject.'</title><style type="text/css">body, html{font-family:Arial, Helvetica, sans-serif;color:#666666;font-size:14px;}</style></head><body>'; $emailbody .= $message; $emailbody .= '</body></html>'; mail($friends_email, $subject, $emailbody, $headers); endforeach; Quote Link to comment Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 Thanks for the reply cags, I got the same out come, it sends all 3 emails but they are all the same email. Quote Link to comment Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 What does your HTML look like? Quote Link to comment Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 <form action="" method="post" name="form1"> <input type="hidden" id="id" value="1"> <input type="hidden" name="form" value="get_involved" /> <input name="submit_email" type="hidden" value="1" /> Your Full Name:<br /> <input type="text" name="full_name" id="full_name" class="input" /> Your Email Address:<br /> <input type="text" name="email" id="email" class="input" /> <table width="100%" border="0" cellspacing="4" cellpadding="0"> <tbody id="divTxt"> </tbody> <tfoot> <tr> <td><a href="return false;" onClick="addFormField(); return false;"><img src="components/com_tell_a_friend/SpryAssets/add.png" alt="" border="0" /></a></td> </tr> </tfoot> </table> <input type="submit" name="button" id="button" value="Submit" class="button<?PHP if($_POST['submit_email']=="1"){ echo '_disabled"';} ?>" <?PHP if($_POST['submit_email']=="1"){ echo 'disabled="disabled"';} ?> /> </form> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script> <script type="text/javascript" src="components/com_tell_a_friend/SpryAssets/add-remove.js"></script> Quote Link to comment Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 Here is my jquery code: // ADD A FIELD function addFormField(){ var id = document.getElementById("id").value; $("#divTxt").append('<tr id="row' + id + '" class="field"><td><input type="text" name="fname[]" id="fname' + id + '" class="input" value="Full Name" onfocus="if(this.value==\'Full Name\'){this.value=\'\'};" onblur="if(this.value==\'\'){this.value=\'Full Name\'};" /> <input type="text" name="femail[]" id="femail' + id + '" class="input" value="Email Address" onfocus="if(this.value==\'Email Address\'){this.value=\'\'};" onblur="if(this.value==\'\'){this.value=\'Email Address\'};" /> <a href=\'return false;\' onClick=\'removeFormField("#row' + id + '"); return false;\' class="remove"><img src="../components/com_tell_a_friend/SpryAssets/remove.png" alt="" border="0" /></a></td></tr>'); $('#row' + id).highlightFade({ color:'green_yellow', speed:500 }); id = (id - 1) + 1; document.getElementById("id").value = id; } // REMOVE A FIELD function removeFormField(id){ $(id).remove(); } Quote Link to comment Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 I know nothing about JavaScript/jQuery, but from what I can tell it seems to be ok... Here is a simple example of the process working... <?php if(isset($_POST['submit'])) { foreach($_POST['name'] as $k=>$v) { echo "Sending email to " . $v . " at " . $_POST['email'][$k] . '<br/>'; } } ?> <br/> <form action="" method="post"> <input type="text" name="name[]" value="name 1" /><input type="text" name="email[]" value="email 1" /><br /> <input type="text" name="name[]" value="name 2" /><input type="text" name="email[]" value="email 2" /><br /> <input type="text" name="name[]" value="name 3" /><input type="text" name="email[]" value="email 3" /><br /> <input type="submit" name="submit" /> </form> Also try putting this at the top of your page so you can see what the contents of the $_POST array looks like, the answer may jump out at you. echo '<pre>'; print_r($_POST); echo '</pre>'; Quote Link to comment Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 Ya it looks very simple and I can not see why this is not working grrrr.... So I have added this bit of code in my foreach to see what is getting submitted: foreach($_POST as $key => $value): $friends_name = $_POST['fname'][$i]; $friends_email = $_POST['femail'][$i]; echo $friends_name.' '.$friends_email; and it is displaying this: Joe joe@mydomain.comMike mike@mydomain.comEmily emily@mydomain.com Quote Link to comment Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 Which is exactly what it should read. You can see that on each iteration of the loop the email address is different, since your mail call is... mail($friends_email, $subject, $emailbody, $headers); It should send 1 to each of joe@mydomain.com, mike@mydomain.com and emily@mydomain.com. It should be practically impossible for any other outcome as you can quite clearly see that $friends_email value is changing. Quote Link to comment Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 ok I think it has something to do with this bit of code: $message = str_replace("{friends_name}",$_POST['fname'][$i],$message); $message = str_replace("{friends_email}",$_POST['femail'][$i],$message); $message = str_replace("{your_name}",$_POST['full_name'],$message); $message = str_replace("{your_email}",$_POST['email'],$message); $message = str_replace("{date}",date('l, F jS, Y'),$message); $message = str_replace("{domain}", 'http://'.$_SERVER['HTTP_HOST'],$message); Quote Link to comment Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 $i = 0; foreach($_POST as $key => $value): echo $_POST['fname'][$i].' '.$_POST['femail'][$i]; $subject = $params->get('subject'); $message = str_replace("{friends_name}",$_POST['fname'][$i],$message); $message = str_replace("{friends_email}",$_POST['femail'][$i],$message); $message = str_replace("{your_name}",$_POST['full_name'],$message); $message = str_replace("{your_email}",$_POST['email'],$message); $message = str_replace("{date}",date('l, F jS, Y'),$message); $message = str_replace("{domain}", 'http://'.$_SERVER['HTTP_HOST'],$message); // message $emailbody = 'Hello '.$_POST['fname'][$i].',<br />'; $emailbody .= $message; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: '.$params->get('fromemail').' <'.$params->get('fromemail').'>' . "\r\n"; // Mail it mail($_POST['femail'][$i], $subject, $emailbody, $headers); $i++; endforeach; display the following in the email body: Hello Emily, Hello Joe, Quote Link to comment Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 so can str_replace be used only once per loop? Quote Link to comment Share on other sites More sharing options...
Presto-X Posted October 13, 2009 Author Share Posted October 13, 2009 at last I got it working with the following: $message = $params->get('message'); $i = 0; foreach($_POST as $key => $value): echo $_POST['fname'][$i].' '.$_POST['femail'][$i]; $subject = $params->get('subject'); $message2 = str_replace("{friends_name}",$_POST['fname'][$i],$message); $message2 = str_replace("{friends_email}",$_POST['femail'][$i],$message2); $message2 = str_replace("{your_name}",$_POST['full_name'],$message2); $message2 = str_replace("{your_email}",$_POST['email'],$message2); $message2 = str_replace("{date}",date('l, F jS, Y'),$message2); $message2 = str_replace("{domain}", 'http://'.$_SERVER['HTTP_HOST'],$message2); // message $emailbody = 'Hello '.$_POST['fname'][$i].',<br />'; $emailbody .= $message2; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: '.$params->get('fromemail').' <'.$params->get('fromemail').'>' . "\r\n"; // Mail it mail($_POST['femail'][$i], $subject, $emailbody, $headers); $i++; unset($message2); endforeach; Quote Link to comment Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 Look at it from a logical point of view. Assumably to start with you have somewhere in your code (outside the loop) that says something along the lines of. $message = "Hello {friends_name}, your friend {your_name} ({your_email}) has asked us to send this link... blah..."; Now lets say you enter 3 emails (john (john@domain.com), jim (jim@domain.com) and jane (jane@domain.com). The first time you run the loop you str_replace the values in that string using... $message = str_replace("{friends_name}",$_POST['fname'][$i],$message); $message = str_replace("{friends_email}",$_POST['femail'][$i],$message); $message = str_replace("{your_name}",$_POST['full_name'],$message); $message = str_replace("{your_email}",$_POST['email'],$message); On the second loop of the code your $message variable now reads... $message = "Hello jim, your friend pete (pete@domain.com) has asked us to send this link... blah..."; So your str_replace has no effect on the string. The fix is, outside of the line to have... $message_template = "Hello {friends_name}, your friend {your_name} ({your_email}) has asked us to send this link... blah..."; $values = array("{friends_name}", "{friends_email}", "{your_name}", "{your_name}"); Then inside the loop use... $replacements = array($_POST['fname'][$key], $_POST['femail'][$key], $_POST['full_name'], $_POST['email']); $message = str_replace($values, $replacements, $message_template); EDIT: Ok you fixed it, but you can probably neaten it up using the arrays. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.