/    Sign up×
Community /Pin to ProfileBookmark

explode & for loop

I have an email form that has possible email addresses to send to designated by checkboxes. I already have them in an array and it grabs all of them fine, I can echo them to the screen but I can’t seem to get them to explode and loop through them propery to format them to send.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@JonaApr 27.2004 — [font=arial]Think you could show some code? I can't help you very well without any code. :rolleyes: [/font]
Copy linkTweet thisAlerts:
@violent_jApr 27.2004 — hmm .. i can't quite understand what you mean, you say that you have all the addressess in an array and that you can echo them to the screen, but you can't use them to send email ?

grtz
Copy linkTweet thisAlerts:
@mikeyzcauthorApr 27.2004 — here is the code. I think the problem is that the "$value" isn't being exploded and looped through.


[code=php]
<%
/* ------------------------------------------------------------------------- */


if($_POST['submit'])
{
if(isset($_POST['checkbox']))
{
$emails = $_POST['checkbox'];
echo "The follwoing email addresses have been selected:<br />";
foreach($emails as $key => $value)

{
echo "$value <br />";

}


}
else
{
echo "no email address selected";
}
{

}

}
function send_mail ( $Data )
{

$To = $value
$Subject = "Request for Programming change";
$Msg = stripcslashes ( $Data["client"] . "n" .
$Data["issue"] . "n" .
$Data["change"] . "n" .
$Data["timeframe"] . "n" .
$Data["price"] . "n");
$From = "From: " . stripcslashes ( $Data["mail"] );
$Headers = trim ( $From . "n" );

/* Send the email using the SMTP mail() */
mail ( $To, $Subject, $Msg, $Headers );
}
%>
<html>
<head>
<title>Request for Programming Change</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<? send_mail ( $HTTP_POST_VARS ); ?>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@NevermoreApr 27.2004 — If you have them as an array you don't need to use explode; that's only if they're a single string. If they are an array. just substitute your array name into the implode.
[code=php]
$subject="hi";
$body="body text";
$headers="From: [email][email protected][/email]";
$to=implode(",",$email_array);
mail($to,$subject,$body,$headers);

[/code]


If you aren't actually using an array, you need to turn it into an array, or replace characters to make a valid 'to' list. For example, if the email addresses are separated by spaces, just use $email_array=explode(" ",$your_string); to create the array for the preceding example.

Need more help? Just post.
Copy linkTweet thisAlerts:
@NevermoreApr 27.2004 — Looks like you did post, and beat me to it. Try this: (assumes that everything else works)
[code=php]
<%
/* ------------------------------------------------------------------------- */


if($_POST['submit'])
{
if(isset($_POST['checkbox']))
{
$emails = $_POST['checkbox'];
echo "The following email addresses have been selected:<br />";
foreach($emails as $key => $value)

{
echo "$value <br />";

}


}
else
{
echo "no email address selected";
}
{

}

}
function send_mail ( $Data )
{

$To = implode(",",$_POST['emails']);
$Subject = "Request for Programming change";
$Msg = stripcslashes ( $Data["client"] . "n" .
$Data["issue"] . "n" .
$Data["change"] . "n" .
$Data["timeframe"] . "n" .
$Data["price"] . "n");
$From = "From: " . stripcslashes ( $Data["mail"] );
$Headers = trim ( $From . "n" );

/* Send the email using the SMTP mail() */
mail ( $To, $Subject, $Msg, $Headers );
}
%>
<html>
<head>
<title>Request for Programming Change</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<? send_mail ( $HTTP_POST_VARS ); ?>
</body>
</html> [/code]

In PHP variables aren't available to functions unless they're global, and after using the foreach you will only be left with one email address to send to rather than all of them. Also note that tags like those could cause problems on other systems, you should probably change to <?php and ?>, unless you have a legitimate reason not to.
×

Success!

Help @mikeyzc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...