2013-11-01 26 views
0

UPDATE:iframe解決方案的工作:如何在不重定向的情況下執行表單操作?

<form action="script.php" **target="aniFrame"** method="post" enctype="multipart/form-data"> 
    <label for="file">Package:</label> 
    <input type="file" name="file" id="file"><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

<iframe name="aniFrame"></iframe> 

我執行PHP腳本來發送電子郵件,但不希望用戶在成功進行重定向。我怎樣才能做到這一點? (我的腳本中有一個彈出框,提示「成功!」)謝謝!

<form action="script.php" method="post" enctype="multipart/form-data"> 
    <label for="file">Package:</label> 
    <input type="file" name="file" id="file"><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

添加我的PHP代碼:

<?php 
/** 
* Simple example script using PHPMailer with exceptions enabled 
* @package phpmailer 
* @version $Id$ 
*/ 

require '../class.phpmailer.php'; 

try { 
    $mail = new PHPMailer(true); //New instance, with exceptions enabled 

    $body    = "Hello, World!"; 

    $mail->IsSMTP();       // tell the class to use SMTP 
    $mail->SMTPAuth = true;     // enable SMTP authentication 
    $mail->Port  = 12;     // set the SMTP server port 
    $mail->Host  = "mail.blah.org"; // SMTP server 
    $mail->Username = "[email protected]";  // SMTP server username 
    $mail->Password = "pwd";   // SMTP server password 

    //$mail->IsSendmail(); // tell the class to use Sendmail 

    $mail->AddReplyTo("[email protected]","First Last"); 

    $mail->From  = "[email protected]"; 
    $mail->FromName = "First Last"; 

    $to = "[email protected]"; 

    $mail->AddAddress($to); 

    $mail->Subject = "First PHPMailer Message"; 

    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 
$mail->WordWrap = 80; // set word wrap 

    $mail->MsgHTML($body); 

    $mail->IsHTML(true); // send as HTML 

    $mail->Send(); 
    echo "<script type='text/javascript'>alert('submitted successfully!')</script>"; 

    header("HTTP/1.0 204 No Response"); //added this, works but no popup... 
} 
catch (phpmailerException $e) { 
    echo $e->errorMessage(); 
} 

>

+1

AJAX是最好的選擇,然後 – Ashish

回答

1

最簡單的方法是讓服務器返回一個204 No Content HTTP response

或者,將表單的target attribute設置爲(i)框架。

或者,使用JavaScript到capture the submit event的表單,prevent the default behaviour,並使HTTP請求using XMLHttpRequest

+0

添加204標題工作,以使其不重新直接,但現在我的成功彈出框不顯示...有什麼建議麼? – KateMak

+0

如果您想對響應的內容做些什麼,那麼您必須在響應中發送一些內容。要麼使用第一個選擇,要麼將警報邏輯移動到帶有表單的頁面的JS,並使用第二個選項。 – Quentin

+0

對不起......有點困惑,你可以發佈一些代碼,這樣我可以更好地理解這兩種選擇?非常感謝您的幫助! – KateMak

0

點擊提交按鈕發送ajax調用。並在Ajax的成功只是顯示彈出框說成功..

相關問題