Need help with select option

Hi all
Hi have a form with a select button for a request to 3 differents departments.
Thank you for the help.

Your message has been sent. Thank you!
Départements Comptabilité Répartiteur Ventes
Envoyer

Here is my PHP that I would to use to send the email to the differents departments.

<?php /* THIS FILE USES PHPMAILER INSTEAD OF THE PHP MAIL() FUNCTION */ require 'PHPMailer-master/PHPMailerAutoload.php'; /* * CONFIGURE EVERYTHING HERE */ // an email address that will be in the From field of the email. $mail->CharSet="UTF-8"; $fromEmail = '[email protected]'; /* company website*/ $fromName = 'Site web'; // subject of the email $subject = 'Nouveau message fomulaire du siteweb'; // form field names and their translations. // array variable name => Text to appear in the email $fields = array('nom_id' => 'Nom', 'email_id' => 'Courriel', 'depart_id' => 'Adressé à', 'message_id' =>'Message'); $mail->Body = array('nom_id' => 'Nom -', 'email_id' => 'Courriel -', 'depart_id'=> 'Adressé à', 'message_id' =>'Message'); //, 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message' // message that will be displayed when everything is OK :) $okMessage = 'Votre demande a été soumis avec succès. Merci, I will get back to you soon!'; // If something goes wrong, we will display this message. $errorMessage = 'There was an error while submitting the form. Please try again later'; /* * LET'S DO THE SENDING */ // if you are not debugging and don't need error reporting, turn this off by error_reporting(0); error_reporting(E_ALL & ~E_NOTICE); try { if(count($_POST) == 0) throw new \Exception('Form is empty'); $emailTextHtml = "

Message du formulaire contacte du siteweb


"; $emailTextHtml .= ""; foreach ($_POST as $key => $value) { // If the field exists in the $fields array, include it in the email if (isset($fields[$key])) { $emailTextHtml .= ""; } } $emailTextHtml .= "
$fields[$key] $value

"; $emailTextHtml .= "

Merci!
Passez une belle journée.

"; $mail = new PHPMailer; $mail->setFrom($fromEmail, $fromName); $mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress(); $mail->addReplyTo($from); $mail->isHTML(true); $mail->CharSet="UTF-8"; $mail->Subject = $subject; $mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy if(!$mail->send()) { throw new \Exception('I could not send the email.' . $mail->ErrorInfo); } $responseArray = array('type' => 'success', 'message' => $okMessage); } catch (\Exception $e) { // $responseArray = array('type' => 'danger', 'message' => $errorMessage); $responseArray = array('type' => 'danger', 'message' => $e->getMessage()); } // if requested by AJAX request return JSON response if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $encoded = json_encode($responseArray); header('Content-Type: application/json'); echo $encoded; } // else just display the message else { echo $responseArray['message']; }

You have posted in the help forums but your post doesn’t seem to contain any questions. You should also make sure your code is properly formatted and use use the code tag </> when posting code

I have a form with a select button for different support department. I would like when a user select department3 is request would go directly to that department. I can’t get it working.

This is my select option in my form

Contact support
Contact department 1
Contact department 2
Contact department 3

indent preformatted text by 4 spaces
<?php // First you check if the button submit is been clicked if (isset($_POST['submit']) ) { $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $message = $_POST['message']; $from = 'From contact form'; if( $_POST['department'] == "support" ) { $subject = 'Mail to customer support'; $to = "[email protected]"; header("Location: www.domain.com/supportSuccess.html"); } // You could better wrap this into a switch case statement. else if( $_POST['department'] == "department1" ) { $subject = 'Mail to department1'; $to = "[email protected]"; header("Location: www.domain.com/departmentSucess.html"); } else if( $_POST['department'] == "department2" ) { $subject = 'Mail to department2'; $to = "[email protected]"; header("Location: www.domain.com/departmentSucess.html"); } else if( $_POST['department'] == "department3" ) { $subject = 'Mail to department3'; $to = "[email protected]"; header("Location: www.domain.com/departmentSucess.html"); } $body = "$name\n $email\n $phone\n $message"; if (mail ($to, $subject, $body, $from)) { header("Location:www.google.com"); } else { echo '

Seems like something went wrong!

'; } } ?>

First of all, I always suggest using a mail library like PHPMailer or something else. In my experience it’s more reliable. If the computer you are sending the mail from is not properly configured as a mail server your e-mail will probably go into the junk mail folder of the recipient, if it gets delivered at all.

` ` `
<?php echo "put your code"; ?>
<?php echo "in between 3 backticks like this"; ?>
<?php echo "... but no spaces between the backtics:"; ?>
<?php echo "use  (```) instead of (` ` `) "; ?>
` ` `

Here is a demo of using PHPMailer along with a gmail account for sending mail.

Tried to properly format your code:

<?php 
// First you check if the button submit is been clicked 
if (isset($_POST['submit']) ) { 
	$name = $_POST['name'];
	$email = $_POST['email']; 
	$phone = $_POST['phone']; 
	$message = $_POST['message']; 
	$from = 'From contact form'; 

	if( $_POST['department'] == "support" ) {
		$subject = 'Mail to customer support'; $to = "[email protected]"; header("Location: www.domain.com/supportSuccess.html"); } 
	
	// You could better wrap this into a switch case statement. 
	else if( $_POST['department'] == "department1" ) { 
		$subject = 'Mail to department1'; 
		$to = "[email protected]"; 
		header("Location: www.domain.com/departmentSucess.html"); 
	}
	else if( $_POST['department'] == "department2" ) { 
		$subject = 'Mail to department2'; 
		$to = "[email protected]"; 
		header("Location: www.domain.com/departmentSucess.html"); 
	} 
	else if( $_POST['department'] == "department3" ) { 
		$subject = 'Mail to department3'; 
		$to = "[email protected]"; 
		header("Location: www.domain.com/departmentSucess.html");
	} 

	$body = "$name\n $email\n $phone\n $message"; 
	if (mail ($to, $subject, $body, $from)) {
		header("Location:www.google.com"); 
	} 
	else { 
		echo 'Seems like something went wrong!'; 
	} 
} 
?>
1 Like
<?php 

$meta = array(
  'support'     => array( 'name'=>'customer support',     'to'=>'[email protected]','redirect'=>'supportSuccess.html'   ),
  'department1' => array( 'name'=>'department1 support',  'to'=>'[email protected]',    'redirect'=>'departmentASucess.html'),
  'department2' => array( 'name'=>'department2 help',     'to'=>'[email protected]',    'redirect'=>'departmentBSucess.html'),
  'department3' => array( 'name'=>'department3 operations','to'=>'[email protected]', 'redirect'=>'departmentCSucess.html'),
);


if( isset($_POST['submit']) && isset( $meta[ $_POST['department'] ] ) )
{
	$data = (object) $_POST;
	
	$use = (object) $meta[ $_POST['department'] ];
	
	$body = "$data->name\n $data->email\n $data->phone\n $data->message"; 

	if( mail($use->to, "Mail to $use->name", $body, 'From contact form') )
	{
		header("Location: $use->redirect"); 
	} 
	else { 
		echo 'Seems like something went wrong!'; 
	}
} 
?>

Thank you for the prompt reply. The $meta=array() is working fine. Now I receiving a email with : ´´From contact form’´ body with no other info.

Here is a copy of html form

                  <form name="sentMessage" form action="contact3.php" method="post" enctype="multipart/form-data" id="contactForm" novalidate>
                    <div class="col-md-6">
                       <div class="form-group">
                          <input type="text" class="form-control" placeholder="Your name *" id="name" required data-validation-required-message="Write your name">
                          <p class="help-block text-danger"></p>
                       </div>
                       <div class="form-group">
                          <input type="email" class="form-control" placeholder="Your email *" id="email" required data-validation-required-message="Write your email">
                          <p class="help-block text-danger"></p>
                       </div>
                       <div class="form-group">
                          <input type="tel" class="form-control" placeholder="Your phone number *" id="phone" required data-validation-required-message="Write your phone number">
                       </div>
                       <div class="form-group">
                          <select name="department" class="form-control">
                             <option value="support">Contact support</option>
                             <option value="department1">Contact department 1</option>
                             <option value="department2">Contact department 2</option>
                             <option value="department3">Contact department 3</option>
                          </select>
                       </div>
                    <div class="col-md-6">
                       <div class="form-group">
                          <textarea class="form-control" placeholder="Message *" id="message" required data-validation-required-message="Type a message"></textarea>
                       </div>
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-lg-12 text-center">
                       <div id="success"></div>
                       <button type="submit" name="submit" class="btn btn-xl">Send E-mail</button>
                    </div>
                 </div>
              </form>

Thank you for the help, I got it working

Sponsor our Newsletter | Privacy Policy | Terms of Service