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

Php 如何在没有重定向的情况下执行表单操作?,php,html,redirect,popup,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

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

最简单的选择是让服务器返回一个

或者,将表单的格式设置为(i)帧


或者,使用JavaScript为表单创建一个ajax调用,并发出HTTP请求。

单击submit按钮发送ajax调用。在Ajax成功时,只需显示一个弹出框,上面写着success..

Ajax是最好的选择,然后添加204标题可以使其不重新定向,但现在我的成功弹出框没有显示…任何建议?如果你想对响应的内容做些什么,那么你必须在响应中发送一些内容。或者使用第一个选项,或者将你的警报逻辑移动到JS,然后使用第二个选项。对不起……有点困惑,你能发布一些代码让我更好地理解这两个选项吗?非常感谢你的帮助!