感谢使用PHP发送邮件时的状态

时间:2015-04-20 07:48:31

标签: php forms email

我希望在使用PHP发送邮件时显示感谢信息

我试过像

这样的东西
if(mail){echo "<script type='text/javascript'>alert('bedankt!');</script>";}

但我无法得到警报。 我将表单定向到mail.php,但是当mail.php完成后,它再次重定向到index.html header('Location: ' . $_SERVER['HTTP_REFERER']);

这可能是它没有显示警报的原因吗?或者我做错了什么?

修改

我的代码:

        <form action="mail.php" method="post" onsubmit="bedanktBericht()">
            <input class="invoeren" name="naamUser" type="text" placeholder="Naam" required>
            <input class="invoeren" name="nummerUser"  type="text" placeholder="Telefoonnummer" required>
            <input class="invoeren" name="onderwerpUser"  type="text" placeholder="Onderwerp" required>
            <textarea class="invoeren" type="text" name="opmerkingUser"  placeholder="Opmerking" required></textarea>           
            <button type="submit" id="contactButton" >Verstuur</button>
        </form>

<?php error_reporting(E_ALL); ini_set('display_errors', 1); 
ob_start(); 

$naamUser = $_POST['naamUser'];
$nummerUser = $_POST['nummerUser'];
$onderwerpUser = $_POST['onderwerpUser'];
$opmerkingUser = $_POST['opmerkingUser'];


$formcontent="Aanvraag formulier Domein naam \n
Naam: $naamUser \n
Telefoonnummer: $nummerUser \n
Onderwerp: $onderwerpUser \n
Opmerking: $opmerkingUser \n              
              ";              

$recipient = "email@email.nl";

$subject = "Terug Bellen";

$mailheader = "From:  \r\n";

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");


header('Location: ' . $_SERVER['HTTP_REFERER']); /* Stuur de browser naar www.site.nl */  

if(mail){echo "<script type='text/javascript'>alert('bedankt!');</script>";}
?>

编辑2: 我想在index.html页面上执行警报。 不是mail.php。这就是我在重定向后尝试使用echo的原因。

2 个答案:

答案 0 :(得分:0)

mail.php 上这样做可能会解决:

if(mail(paramiters...)){
    header('location:index.html?msg=successfull');
}

现在在 index.html

<script>
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var succ = getParameterByName('msg');
if(succ=='successfull'){
    alert('bedankt!');
}
</script>

答案 1 :(得分:0)

问题是您在警报之前重定向用户:

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

header('Location: ' . $_SERVER['HTTP_REFERER']); // Redirect

// This code is never reached
if(mail){echo "<script type='text/javascript'>alert('bedankt!');</script>";}

此外,您的if语句写错了。这样的事情会更有意义:

if (mail($recipient, $subject, $formcontent, $mailheader)) {
    echo "<script type='text/javascript'>alert('bedankt!');</script>";
    header('Location: ' . $_SERVER['HTTP_REFERER']); // Redirect
} else {
    die("Error!");
}

然而,echo并不是解决这个问题的正确方法,我怀疑你是否可以这样做。

尝试查看Ajax,我认为这更适合您的问题。您也可以找到一些解释here