如何在不重定向的情况下执行表单操作?

时间:2013-11-01 06:45:35

标签: php html redirect popup

更新: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();
}

&GT;

2 个答案:

答案 0 :(得分:1)

最简单的选择是让服务器返回204 No Content HTTP response

或者,将表单的target attribute设置为(i)帧。

或者,对表单capture the submit event使用JavaScript prevent the default behaviour,然后发出HTTP请求using XMLHttpRequest

答案 1 :(得分:0)

点击提交按钮发送ajax电话。而在Ajax的成功之后,只需显示弹出框即成功..

相关问题