Jump to content

How do I Send Form Content to Checked Emails, and then Refresh to Blank Form?


PrimeR

Recommended Posts

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

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

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

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

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

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

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

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

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

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.