PrimeR Posted April 9, 2010 Share Posted April 9, 2010 My current code is not working... Here is my checkbox HTML code: <input type="checkbox" name="salesperson1" value="nameone@domain.com"> Fred Smith<br /> <input type="checkbox" name="salesperson2" value="nametwo@domain.com"> Zed Phillips<br /> <input type="checkbox" name="salesperson3" value="namethree@domain.com"> Jane Doe<br /> Here is my checkbox and ending PHP code: if(isset($_POST['salesperson1'])) { $EmailTo = $_POST['salesperson1']; if(isset($_POST['salesperson2'])) { $EmailTo = $_POST['salesperson2']; if(isset($_POST['salesperson3'])) { $EmailTo = $_POST['salesperson3']; } $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=form.php\">"; } else{ print "Please try again."; } ?> All help is very much appreciated! Link to comment Share on other sites More sharing options...
the182guy Posted April 9, 2010 Share Posted April 9, 2010 Use this for debugging // at the start of your script error_reporting(E_ALL); init_set('display_errors', '1'); There are two fatal errors in that script - you are not closing your if statement brackets { } for the first two salespeople. It still won't work as you expect even with that fixed. You are overriting the $EmailTo variable each time, so if all three salespeople are checked, only the last one will receive the email. You need to append to the variable $EmailTo and use a comma to seperate email addresses. Link to comment Share on other sites More sharing options...
PrimeR Posted April 12, 2010 Author Share Posted April 12, 2010 You need to append to the variable $EmailTo and use a comma to seperate email addresses. I don't quite understand this part. Could you give me an example of what the correct code would look like? Link to comment Share on other sites More sharing options...
PrimeR Posted April 12, 2010 Author Share Posted April 12, 2010 Could someone read the previous posts and show code example illustrating the last point? (I really appreciate anyone's help.) Link to comment Share on other sites More sharing options...
newbtophp Posted April 12, 2010 Share Posted April 12, 2010 Read what the 182guy said, adding them two lines should have revealed the problem to you, aswell as quote the lines. Your not ending the brackets for your if statements correctly, you start it but don't end/close it... <?php if (isset($_POST['salesperson1'])) { $EmailTo = $_POST['salesperson1']; } elseif (isset($_POST['salesperson2'])) { $EmailTo = $_POST['salesperson2']; } elseif (isset($_POST['salesperson3'])) { $EmailTo = $_POST['salesperson3']; } $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); if ($success) { print "<meta http-equiv=\"refresh\" content=\"0;URL=form.php\">"; } else { print "Please try again."; } ?> Link to comment Share on other sites More sharing options...
PrimeR Posted April 12, 2010 Author Share Posted April 12, 2010 I understand the part about adding closing brackets. It's this part that I'm having trouble with: "It still won't work as you expect even with that fixed. You are overriting the $EmailTo variable each time, so if all three salespeople are checked, only the last one will receive the email. You need to append to the variable $EmailTo and use a comma to seperate email addresses." Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 12, 2010 Share Posted April 12, 2010 I understand the part about adding closing brackets. It's this part that I'm having trouble with: "It still won't work as you expect even with that fixed. You are overriting the $EmailTo variable each time, so if all three salespeople are checked, only the last one will receive the email. You need to append to the variable $EmailTo and use a comma to seperate email addresses." Pardon any rudeness the following question may imply: do you know PHP? You have 3 if statements in your code. And each one changes the value of the variable $EmailTo. So if all 3 checkboxes are checked, then after the last if statement, $EmailTo will only have the last e-mail address and not all 3. Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 12, 2010 Share Posted April 12, 2010 First, you need to define what you want the code to do. Do you want only one checkbox to be selected at a time or do you want to allow any where from 1 to all 3 checkboxes to be selected? Link to comment Share on other sites More sharing options...
PrimeR Posted April 12, 2010 Author Share Posted April 12, 2010 (Pardon my basic questions. I'm in the process of learning PHP.) I want to allow the user to select anywhere from 1 to all 3 check boxes. Link to comment Share on other sites More sharing options...
newbtophp Posted April 12, 2010 Share Posted April 12, 2010 b] You need to append to the variable $EmailTo and use a comma to seperate email addresses.[/b]" Your could try: <?php if (isset($_POST['salesperson1'])) { $EmailTo = $_POST['salesperson1']; if (isset($_POST['salesperson2'])) { $EmailTo .= ','.$_POST['salesperson2']; if (isset($_POST['salesperson3'])) { $EmailTo .= ','.$_POST['salesperson3']; } $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); if ($success) { print "<meta http-equiv=\"refresh\" content=\"0;URL=form.php\">"; } else { print "Please try again."; } ?> Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 12, 2010 Share Posted April 12, 2010 Here's a hint to a better solution: <input type="checkbox" name="salesperson[]" value="nameone@domain.com"> Fred Smith<br /> <input type="checkbox" name="salesperson[]" value="nametwo@domain.com"> Zed Phillips<br /> <input type="checkbox" name="salesperson[]" value="namethree@domain.com"> Jane Doe<br /> See if you can figure that out. @newbtophp, you're missing some closing braces in your if statements. Link to comment Share on other sites More sharing options...
Psycho Posted April 12, 2010 Share Posted April 12, 2010 That ^^ code (EDIT: newbtophp''s code)would add a comma to the very beginning if the first checkbox is not checked. A better approach is to use an array with implode. However, the "quick" fix and the "right" fix are two different things. The right fix would be to change the names of the checkboxes to "salespersons[]" instead of naming them "salesperson1", "salesperson2", etc. By giving them the same name, they will be treated as an array and you could do this: if (isset($_POST['salesperson1'])) { $EmailTo = implode(',', $_POST['salespersons']); } With your current code you could do this: $emails = array(); if(isset($_POST['salesperson1'])) { $emails[] = $_POST['salesperson1']; } if(isset($_POST['salesperson2'])) { $emails[] = $_POST['salesperson2']; } if(isset($_POST['salesperson3'])) { $emails[] = $_POST['salesperson3']; } $emailTo = implode(',', $emails); Link to comment Share on other sites More sharing options...
newbtophp Posted April 12, 2010 Share Posted April 12, 2010 @mdjmato That ^^ code would add a comma to the very beginning if the first checkbox is not checked. A better approach is to use an array with implode. True did'nt think of it like that, - thanks. Link to comment Share on other sites More sharing options...
Recommended Posts