tlflow Posted January 9, 2008 Share Posted January 9, 2008 Hi All, I'm baffled as to how to do this. No doubt, I'm making this harder than it has to be. Client wants to 10 textboxes that once the send button is clicked it sends a generic email to 10 people. This is all simple until somebody tries just inserting email addresses in say the third box and the ninth box. How do I get my code to skip over all the empty strings (results from empty textboxes) and send the ones that just have actual email addresses in them? I've been reading over foreach in the php manual and it seems like I could use it, but I have no clue as to how to even start with that yet. I've even tried bringing everything over and then exploding it, but that seems pointless and a waste of time - (besides I can't figure out what would happen if the last box is empty). Any ideas? Thanks, Tlflow Quote Link to comment https://forums.phpfreaks.com/topic/85112-how-to-take-out-empty-results-when-emailing/ Share on other sites More sharing options...
btherl Posted January 9, 2008 Share Posted January 9, 2008 Can you post your current code? Then we can suggest how to alter it. Quote Link to comment https://forums.phpfreaks.com/topic/85112-how-to-take-out-empty-results-when-emailing/#findComment-434157 Share on other sites More sharing options...
tlflow Posted January 9, 2008 Author Share Posted January 9, 2008 Sure, Here's what I have so far...mind you, it's not much... $email1 = $_POST['email1']; $email2 = $_POST['email2']; $email3 = $_POST['email3']; $email4 = $_POST['email4']; $email5 = $_POST['email5']; $email6 = $_POST['email6']; $email7 = $_POST['email7']; $email8 = $_POST['email8']; $email9 = $_POST['email9']; $email10 = $_POST['email10']; $mailto = "$email1,$email2,$email3"; echo $mailto; $array = explode(",", $mailto); function isEmpty($array = array()) { foreach ($array as $element) { if (!empty($element)) { } } return true; } Quote Link to comment https://forums.phpfreaks.com/topic/85112-how-to-take-out-empty-results-when-emailing/#findComment-434184 Share on other sites More sharing options...
Ken2k7 Posted January 9, 2008 Share Posted January 9, 2008 <?php // checks if form is submitted, may be edited // depending on your submit button value + name if ($_POST['submit'] == "submit"){ foreach($_POST as $p){ if (!empty($p)){ // may want to use preg_match,eregi,ereg etc to // validate that the input is in fact an email // then send the email }}} ?> Quote Link to comment https://forums.phpfreaks.com/topic/85112-how-to-take-out-empty-results-when-emailing/#findComment-434213 Share on other sites More sharing options...
tlflow Posted January 9, 2008 Author Share Posted January 9, 2008 OK, I kinda get it...I think that's the direction I was going towards, but I still don't get how ito just print out the ones that aren't empty as one string (I guess I shouldve said that earlier). I'm thinking having it print out to one string (for example, $mailto = "email1@yahoo.com, email2@hotmail.com, email3@gmail.com" would be the best way to send it, right? The way I have it now, it would be $mailto = "email1@yahoo.com, , email3@gmail.com" if my user didn't enter anything into box #2. Thanks, Tlflow Quote Link to comment https://forums.phpfreaks.com/topic/85112-how-to-take-out-empty-results-when-emailing/#findComment-434263 Share on other sites More sharing options...
Ken2k7 Posted January 9, 2008 Share Posted January 9, 2008 My way is easier. All you have to do is add in the mail function. Quote Link to comment https://forums.phpfreaks.com/topic/85112-how-to-take-out-empty-results-when-emailing/#findComment-434277 Share on other sites More sharing options...
tlflow Posted January 9, 2008 Author Share Posted January 9, 2008 OK, Ken2K7, maybe I'm just missing it....this is what I have so far: <? // $mailto = "$_POST['email1'], $_POST['email2'], $_POST['email3'], $_POST['email4'], $_POST['email5'], $_POST['email6'], $_POST['email7'], $_POST['email8'], $_POST['email9'], $_POST['email10']"; $email1 = $_POST['email1']; $email2 = $_POST['email2']; $email3 = $_POST['email3']; $email4 = $_POST['email4']; $email5 = $_POST['email5']; $email6 = $_POST['email6']; $email7 = $_POST['email7']; $email8 = $_POST['email8']; $email9 = $_POST['email9']; $email10 = $_POST['email10']; $myname = $_POST['myname'] ; $myemail = $_POST['myemail'] ; // checks if form is submitted, may be edited // depending on your submit button value + name if (!$_POST['submit'] == "submit"){ foreach($_POST as $p){ if (!empty($p)){ // may want to use preg_match,eregi,ereg etc to // validate that the input is in fact an email // then send the email $subject = "Help Make a Miracle Happen!" ; $formurl = "form.php" ; $errorurl = "error.php" ; $thankyouurl = "thankyou.php" ; $uself = 1; $headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ; $http_referrer = getenv( "HTTP_REFERER" ); if (empty($myname) || empty($myemail)) { header( "Location: $errorurl" ); exit ; } if ( ereg( "[\r\n]", $myname ) || ereg( "[\r\n]", $myemail ) ) { header( "Location: $errorurl" ); exit ; } $messageproper = "Hi,\n\n" . "This is a simple message.\n\n" . "Sincerely,\n\n" . "$myname\n\n" ; mail($mailto, $subject, $messageproper, "From: \"$myname\" <$myemail>" . $headersep . "Reply-To: \"$myname\" <$myemail>" . $headersep . "X-Mailer: chfeedback.php 2.07" ); header( "Location: $thankyouurl" ); exit ; }}} ?> Quote Link to comment https://forums.phpfreaks.com/topic/85112-how-to-take-out-empty-results-when-emailing/#findComment-434336 Share on other sites More sharing options...
Ken2k7 Posted January 9, 2008 Share Posted January 9, 2008 Try this: (Read my comment) <?php // must have the submit's input attr name to be "submit" and value to be "submit" if ($_POST['submit'] == "submit"){ $errorurl = "error.php"; if (empty($_POST['myname']) || empty($_POST['myemail']) || !preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i",$_POST['email'])){ header("Location: $errorurl"); exit; } else { $myname = mysql_real_escape_string($_POST['myname']); $myemail = mysql_real_escape_string($_POST['myemail']); } foreach($_POST as $p){ if (!empty($p)){ $subject = "Help Make a Miracle Happen!" ; $formurl = "form.php" ; $thankyouurl = "thankyou.php" ; $uself = 1; $headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ; $messageproper = "Hi,\r\n\r\n" . "This is a simple message.\r\n\r\n" . "Sincerely,\r\n\r\n" . "$myname\r\n\r\n" ; // Ken2k7: $mailto is undefined up to this point // mail($mailto, $subject, $messageproper, "From: \"$myname\" <$myemail>" . $headersep . "Reply-To: \"$myname\" <$myemail>" . $headersep . "X-Mailer: chfeedback.php 2.07" ); header( "Location: $thankyouurl" ); exit ; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85112-how-to-take-out-empty-results-when-emailing/#findComment-434347 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.