Jump to content

[SOLVED] PHP Form to Multiple Email Addresses based on Radio Button Selection


Alexs

Recommended Posts

Hi everyone,

 

I need to have a form that will post to different email addresses based on the radio button selection in the form I have created.

 

The form has the following code:

 

<p><b>Name:</b>

                <input name="Name" type="text" id="Name" size="20">

              </p>

              <p> <b>Email Address:</b>

                <input name="Email" type="text" id="Email" size="20">

              <p>

                <input type="radio" name="Query" value="General" checked>

                <b>General Query </b></p>

              <p>

                <input type="radio" name="Query" value="Membership">

                <b>Membership Query </b></p>

              <p>

                <input type="radio" name="Query" value="Coaching">

                <b>Coaching Query </b></p>

              <p>

                <input type="radio" name="Query" value="Events">

                <b>Events Query </b></p>

            </td>

</tr>

<tr>

            <td>

              <p align="left">

                <textarea name="Textarea" cols="50" rows="5">Please fill in your query here....</textarea><br>

              </p>

 

 

and the php file has the following:

 

<?php

 

$name = $_POST["Name"];

$email = $_POST["Email"];

$subject = $_POST["Query"];

$general = $_POST["C1"]; //gene

$membership = $_POST["C2"]; //mem

$coaching = $_POST["C3"]; //coach

$events = $_POST["C4"]; //event

$message = $_POST["Textarea"];

 

$subject = "Club - $subject Query";

 

$emailmessage = "Contacts Name: $name\nEmail Address: $email\nQuery Type: $subject\nQuery is: $message";

 

if ($name!=null && $email!=null && $message!=null)

  {$messagesent = mail($to = "[email protected]", $subject, $emailmessage,"From: $email\r\n" . "Reply-To: $email");

include "./querysent.shtml";}

 

else include "./queryfailed.shtml";

 

?>

 

 

How do i get the form to post to different email addresses based on the radio button selection in the form?

 

Thanks

 

Alex

 

 

It's quite simple to do:

 


$emailgroup = $_POST['QUERY'];

### use ifs to dynamically set the email variable

if($emailgroup == "group1"){ 

$email = "[email protected]"

}

etc etc


 

Don't forget, clean your user input, especially with contact forms. They can be used for spamming, so ideally a captcha should be implemented.

 

at very least filter the input.

 

Hope that helps.

I have managed to get the form to work now and email different addresses depending on the radio button selected but how do I prevent it sending emails if the following fields are not filled in:

  • name
  • email
  • textarea

 

My code now looks like below:

 

<?php

 

$name = $_POST["Name"];

$email = $_POST["Email"];

$subject = $_POST["Query"];

$general = $_POST["General"]; //gene

$membership = $_POST["Membership"]; //mem

$coaching = $_POST["Coaching"]; //coach

$events = $_POST["Events"]; //event

$message = $_POST["Textarea"];

 

$subject = "Club - $subject Query";

$emailgroup = $_POST["Query"];

$emailmessage = "Contacts Name: $name\nEmail Address: $email\nQuery Type: $subject\nQuery is: $message";

$email1 = "[email protected]";

$email2 = "[email protected]";   

 

if ($emailgroup == "General")

{$messagesent = mail($email1, $subject, $emailmessage,"From: $email\r\n" . "Reply-To: $email");

include "./querysent.shtml";}

elseif

  ($emailgroup == "Membership")

{$messagesent = mail($email2, $subject, $emailmessage,"From: $email\r\n" . "Reply-To: $email");

include "./querysent.shtml";}

elseif ($emailgroup == "Coaching")

{$messagesent = mail($email1, $subject, $emailmessage,"From: $email\r\n" . "Reply-To: $email");

include "./querysent.shtml";}

elseif ($emailgroup == "Events")

{$messagesent = mail($email1, $subject, $emailmessage,"From: $email\r\n" . "Reply-To: $email");

include "./querysent.shtml";}

else include "./queryfailed.shtml";

?>

 

I previously had ($name!=null && $email!=null && $message!=null) to do the checking but how do i fit this into my php above?

 

Thanks

 

Alex

 

 

Archived

This topic is now archived and is 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.