Reputation: 61
PHP Rookie here...
Trying to put an if/elseif statement into my php code for a form email.
If user chooses "Sandy" then the email should go to Sandy's email address. If user chooses "Steve" send to Steve's email address, etc. Here is the code I have so far. Any advise is appreciated.
<?php
require_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
if( isset( $_POST['template-contactform-submit'] ) AND $_POST['template-contactform-submit'] == 'submit' ) {
if( $_POST['template-contactform-name'] != '' AND $_POST['template-contactform-email'] != '' AND $_POST['template-contactform-subject'] != '' AND $_POST['template-contactform-message'] != '' ) {
$name = $_POST['template-contactform-name'];
$email = $_POST['template-contactform-email'];
$subject = $_POST['template-contactform-subject'];
$message = $_POST['template-contactform-message'];
$botcheck = $_POST['template-contactform-botcheck'];
if( $_POST['template-contactform-employee'] = 'Sandy' ):
{
$toemail = '[email protected]';
$toname = 'Sandy';
}
elseif( $_POST['template-contactform-employee'] = 'Steve' ):
{
$toemail = '[email protected]';
$toname = 'Steve';
}
elseif( $_POST['template-contactform-employee'] = 'Nick' ):
{
$toemail = '[email protected]';
$toname = 'Nick';
}
else: {
$toemail = "[email protected]";
$toname = "Support";
}
endif;
$body = "$message";
if( $botcheck == '' ) {
$mail->SetFrom( $email , $name );
$mail->AddReplyTo( $email , $name );
$mail->AddAddress( $toemail , $toname );
$mail->Subject = $subject;
$mail->MsgHTML( $body );
$sendEmail = $mail->Send();
if( $sendEmail == true ):
echo '<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">×</button>We have <strong>successfully</strong> received your Message and will get Back to you as soon as possible.</div>';
else:
echo '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button>Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.<br /><br /><strong>Reason:</strong><br />' . $mail->ErrorInfo . '</div>';
endif;
} else {
echo '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button>Bot <strong>Detected</strong>.! Clean yourself Botster.!</div>';
}
} else {
echo '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button>Please <strong>Fill up</strong> all the Fields and Try Again.</div>';
}
} else {
echo '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button>An <strong>unexpected error</strong> occured. Please Try Again later.</div>';
}
?>
Upvotes: 1
Views: 526
Reputation: 61
This solution from @andrewsi worked. Thanks for spotting my error!
if( $_POST['template-contactform-employee'] = 'Sandy')
is wrong - you're using =
for an equality check, rather than ==
. Try if( $_POST['template-contactform-employee'] == 'Sandy')
, and see if that makes a different (you'll need to amend each of the if lines.)
Upvotes: 1