即使在php上提到了“发送”消息,但单击“发送”按钮后,我还是看不到“发送成功”: echo'{“ alert”:“成功”,“ message”:“'。$ message_success。'”}';
代码来自模板,下面是HTML,PHP和JS代码:
HTML:
<div class="contact-form">
<form class="text-white mb-0" id="cf" name="cf" action="include/sendemail.php" method="post">
<div class="row">
<div class="form-process"></div>
<div class="col-12 col-md-6">
<div class="form-group error-text-white">
<input type="text" id="cf-name" name="cf-name" placeholder="Nom" class="form-control fc-bordered fc-light required">
</div>
</div>
<div class="col-12 col-md-6">
<div class="form-group error-text-white">
<input type="email" id="cf-email" name="cf-email" placeholder="Adresse E-mail" class="form-control fc-bordered fc-light required">
</div>
</div>
<div class="col-12 mb-4">
<div class="form-group error-text-white">
<textarea name="cf-message" id="cf-message" placeholder="Message" class="form-control fc-bordered fc-light required" rows="7"></textarea>
</div>
</div>
<div class="col-12 d-none">
<input type="text" id="cf-botcheck" name="cf-botcheck" value="" class="form-control fc-bordered fc-light" />
</div>
<div class="col-12">
<button class="btn btn-white" type="submit" id="cf-submit" name="cf-submit">Send Message</button>
</div>
</div>
</form>
<div class="contact-form-result pt-1"></div>
</div>
PHP:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'phpmailer/src/PHPMailer.php';
// If you intend you use SMTP, uncomment next line
//require 'phpmailer/src/SMTP.php';
$toemails = array();
$toemails[] = array(
'email' => 'xxxx', // Your Email Address
'name' => 'xxxx' // Your Name
);
// Form Processing Messages
$message_success = 'We have successfully received your Message and
will get Back to you as soon as possible.';
$mail = new PHPMailer();
// If you intend you use SMTP, add your SMTP Code after this Line-
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if( $_POST['cf-email'] != '' ) {
$name = isset( $_POST['cf-name'] ) ? $_POST['cf-name'] : '';
$email = isset( $_POST['cf-email'] ) ? $_POST['cf-email'] : '';
$subject = isset( $_POST['cf-subject'] ) ? $_POST['cf-subject'] : '';
$message = isset( $_POST['cf-message'] ) ? $_POST['cf-message'] : '';
$subject = isset($subject) ? $subject : 'New Message From Contact Form';
$botcheck = $_POST['cf-botcheck'];
if( $botcheck == '' ) {
$mail->CharSet = 'UTF-8';
$mail->SetFrom( $email , $name );
$mail->AddReplyTo( $email , $name );
foreach( $toemails as $toemail ) {
$mail->AddAddress( $toemail['email'] , $toemail['name'] );
}
$mail->Subject = $subject;
$name = isset($name) ? "Name: $name<br><br>" : '';
$email = isset($email) ? "Email: $email<br><br>" : '';
$message = isset($message) ? "Message: $message<br><br>" : '';
$referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';
$body = "$name $email $message $referrer";
$mail->MsgHTML( $body );
$sendEmail = $mail->Send();
if( $sendEmail == true ):
echo '{ "alert": "success", "message": "' . $message_success . '" }';
else
echo '{ "alert": "error", "message": "Email could not be sent due to some Unexpected Error. Please Try Again later.<br /><br />Reason:<br />' . $mail->ErrorInfo . '" }';
endif;
} else {
echo '{ "alert": "error", "message": "Bot Detected.! Clean yourself Botster.!" }';
}
} else {
echo '{ "alert": "error", "message": "Please Fill up all the Fields and Try Again." }';
}
} else {
echo '{ "alert": "error", "message": "An unexpected error occured.
Please Try Again later." }';
}
?>
JS:
function ln_contactForm() {
var contactForm = $('.contact-form');
if( contactForm.length < 1 ){ return true; }
contactForm.each( function(){
var el = $(this),
elResult = el.find('.contact-form-result');
el.find('form').validate({
submitHandler: function(form) {
elResult.fadeOut( 500 );
$(form).ajaxSubmit({
target: elResult,
dataType: 'json',
success: function( data ) {
elResult.html( data.message ).fadeIn( 500 );
if( data.alert != 'error' ) {
$(form).clearForm();
setTimeout(function(){
elResult.fadeOut( 500 );
}, 5000);
};
}
});
}
});
});
}
答案 0 :(得分:0)
There might be a syntax error in your below code
if( $sendEmail == true ):
echo '{ "alert": "success", "message": "' . $message_success . '" }';
<b>else</b>
echo '{ "alert": "error", "message": "Email could not be sent due to some Unexpected Error. Please Try Again later. Reason:' . $mail->ErrorInfo . '" }';
endif;
Correct syntax of if else statement is
if( $sendEmail == true ):
else :
endif;
Please fix it in your code, and then try.