2016-06-13 128 views
1

如何在我的表單中單擊發送後顯示成功消息?我的phpmailer工作,但我想也後點擊發送重定向到我的主頁(index.html),並顯示我的消息div。這是我的PHP代碼。PHPmailer如何顯示消息

<?php 
require 'phpmailer/PHPMailerAutoload.php'; 

$mail = new PHPMailer; 

$subject = $_POST['subject']; 
$name = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 

//$mail->SMTPDebug = 3;        // Enable verbose debug output 

$mail->isSMTP();          // Set mailer to use SMTP 
$mail->Host = 'mail.xxxxxxx.pl'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = '[email protected]';     // SMTP username 
$mail->Password = 'password';       // SMTP password 
$mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587;         // TCP port to connect to 

$mail->setFrom($email, $name); 
$mail->addAddress('[email protected]', 'xxxxxxxx');  // Add a recipient    // Name is optional 

$mail->isHTML(true);         // Set email format to HTML 

$mail->Subject = $subject; 
$mail->Body = 'Body test'; 


if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    header('Location: index.html'); 

} 

這是我的jQuery代碼,顯示/隱藏DIV更迭:

$('.box-up').animate({top : 150}, 'normal').delay(3000).animate({top : -500}, 'normal'); 
+0

你必須在某處存儲消息,然後安排你的html頁面來顯示它。 –

回答

0

您可以發送額外的參數,你的要求:

header('Location: index.php?r=1'); 

然後在你的索引頁獲得參數並用它來顯示方框:

<script> 
    var result="<?php echo $_GET['r']; ?>"; 

    if(parseInt(result)){ 
     $('.box-up').animate({top : 150}, 'normal').delay(3000).animate({top : -500}, 'normal'); 
    } 
</script> 

希望這會有所幫助。

+0

所以我必須將index.html保存爲index.php,並將此代碼添加到我的js文件中? –

+0

是的,這是真的... –

+0

好吧,我嘗試了很多次,但這不工作; /嗯... –