Forum Moderators: coopster

Message Too Old, No Replies

Array looping problem

         

jm21

11:30 am on Aug 3, 2009 (gmt 0)

10+ Year Member



Hi, Am new in php and am having a problem with looping through an array. Basically am getting values from a form i.e name and email address, making sure the fields aren't empty then doing a foreach loop to insert the items into a database and send an email to each person. The script works well but for some reason the loop doesnt work well and people receive multiple emails....any idea what's wrong

if (is_array($_POST['f_name']) && is_array($_POST['f_name']) != "" )
{
foreach($_POST['f_name'] as $row=>$Act)
{
$f_name=$_POST['f_name'][$row];
$f_email=$_POST['f_email'][$row];

$Psubject = str_replace ('Recipient', $f_name, $Psubject );
$Pmessage = str_replace ('Recipient', $f_name, $Pmessage );

$headers = "From: $Yemail\r\nReply-To: $Yemail\r\n";
$headers .= "Content-type: text/html\r\n";
$message .= "$Pmessage";
$to = "$f_email" ;
$subject = $Psubject;

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

eelixduppy

12:21 pm on Aug 3, 2009 (gmt 0)



Look at the values of the POSTed variables and see what they look like. Something like this should work just fine:

echo '<pre>';
print_r($_POST['f_name']);
print_r($_POST['f_email']);
echo '</pre>';

If the array lengths don't match or you are getting repeated values in there somehow that may be the source of your errors.

Oh, and Welcome to WebmasterWorld! :)

jm21

1:55 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



I've done that and the problem doesn't seem to be in the array as it captures the information as it is from the form. The weird thing is that once it starts looping the first email goes out allright, however the subsequent emails do not get the person's name - $f_name, instead they got the first person's name, but sends it to the correct email address, i.e. the second persons email address. Can't figure it out

eelixduppy

12:21 am on Aug 4, 2009 (gmt 0)



Try something like this and let me know what happens:

$num = count($_POST['f_name']);
for($i = 0; $i < $num; $i++)
{
$f_name=$_POST['f_name'][$i];
$f_email=$_POST['f_email'][$i];

$Psubject = str_replace ('Recipient', $f_name, $Psubject );
$Pmessage = str_replace ('Recipient', $f_name, $Pmessage );

$headers = "From: $Yemail\r\nReply-To: $Yemail\r\n";
$headers .= "Content-type: text/html\r\n";
$message .= "$Pmessage";
$to = "$f_email" ;
$subject = $Psubject;

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

idfer

3:55 am on Aug 4, 2009 (gmt 0)

10+ Year Member



Here's one problem:

$message .= "$Pmessage";

You're appending to $message with each iteration, so your email messages get longer and longer. Either set $message = '' at the top of your loop or change the statement to this:

$message = "$Pmessage"; // BTW, you don't really need the double-quotes here.

Hope this helps.

omoutop

8:44 am on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In your code you have:

foreach($_POST['f_name'] as $row=>$Act)
{
// blah blah blah
mail($to, $subject, $message, $headers);
}

So, if i read this correclty, for each element in $_POST array, you send 1 email... no?

perhaps you need to construct your message inside your loop, but put the headers/mail part outside the loop

jm21

9:16 am on Aug 4, 2009 (gmt 0)

10+ Year Member



Thanks guys, your tips have helped me figure out where the problem is. First, as idfer pointed out, the message was getting longer with each iteration and I managed to sort that out.
My other problem was that the script is not replacing the name after the first iteration

$Psubject = str_replace ('Recipient', $f_name, $Psubject );
$Pmessage = str_replace ('Recipient', $f_name, $Pmessage );

so once it picks up the name from the first loop it doesnt replace it after the second loop. I've tried unsetting both the $Psubject and $Pmessage after each loop but that doesnt work...any ideas?

idfer

2:45 pm on Aug 4, 2009 (gmt 0)

10+ Year Member



You're overwriting the original text with personalized versions, so after the first iteration 'Recipient' is gone. You want to assign the return value from str_replace() to a different variable. Easiest is to assign the replaced text directly into $subject and $message, like this:

foreach($_POST['f_name'] as $row=>$Act) { 
$f_name = $_POST['f_name'][$row];
$f_email = $_POST['f_email'][$row];

$to = "$f_email" ;
$subject = str_replace('Recipient', $f_name, $Psubject );
$message = str_replace('Recipient', $f_name, $Pmessage );

$headers = "From: $Yemail\r\nReply-To: $Yemail\r\n";
$headers .= "Content-type: text/html\r\n";

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

jm21

4:57 pm on Aug 4, 2009 (gmt 0)

10+ Year Member



Men you guys rock....based on your pointers all i did was change 3 things, replaced
$Psubject = str_replace('Recipient', $f_name, $Psubject );
$Pmessage = str_replace('Recipient', $f_name, $Psubject );

with
$subject = str_replace('Recipient', $f_name, $Psubject );
$message = str_replace('Recipient', $f_name, $Psubject );

and removed the
$message .= "$Pmessage";

and that was it...script works perfectly....thanks alot guys