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@gmail.com", $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

 

 

Link to comment
Share on other sites

It's quite simple to do:

 


$emailgroup = $_POST['QUERY'];

### use ifs to dynamically set the email variable

if($emailgroup == "group1"){ 

$email = "email1@domain.com"

}

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.

Link to comment
Share on other sites

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 = "a1@gmail.com";

$email2 = "a2@hotmail.co.uk";   

 

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.