0

更新: 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   = "hello@bla.org";     // SMTP server username
    $mail->Password   = "pwd";            // SMTP server password

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

    $mail->AddReplyTo("hi@hotmail.com","First Last");

    $mail->From       = "hi@hotmail.com";
    $mail->FromName   = "First Last";

    $to = "hi@gmail.com";

    $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();
}

?>

4

2 回答 2

1

最简单的选择是让服务器返回一个204 No ContentHTTP 响应

或者,将表单的target属性设置为 (i) 框架。

或者,使用 JavaScript捕获表单的submit事件防止默认行为,并使用 XMLHttpRequest发出 HTTP 请求。

于 2013-11-01T06:47:15.993 回答
0

单击提交按钮时发送 ajax 调用。在 Ajax 成功时,只需显示弹出框说 Success..

于 2013-11-01T06:49:17.637 回答