联系表单打开PHP文件而不是显示弹出窗口

时间:2015-02-03 13:18:41

标签: javascript php html css forms

我的网站上有一个联系表格,当前代码会向发件人和收件人发送一封电子邮件,一切正常。但是,一旦完成表单,该页面将打开email.php,而不是显示我希望它会弹出的弹出窗口。我不知道如何修复,因为我不习惯为php和JS编写。以下是我的代码。

<form method="post" action="email.php" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_self" novalidate>
<input type="text" value="" name="full_name" class="fullname" id="mce-name" placeholder="full name" required>
<input type="text" value="" name="phone_num" class="phonenum" id="mce-phone" placeholder="phone number" required>
<br>
<input type="email" value="" name="email" class="email" id="mce-EMAIL" placeholder="email address" required>
<div style="position: absolute; left: -5000px;"><input type="text" name="b_cdb7b577e41181934ed6a6a44_e65110b38d" value=""></div>
<div class="clear"><input type="submit" value="Submit" name="submit" id="mc-embedded-subscribe" class="button"></div>
</form>

    <?php 
if(isset($_POST['submit'])){
    $to = "email@help.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $full_name = $_POST['full_name'];
    $phone_num = $_POST['phone_num'];
    $subject = "Title";
    $subject2 = "Copy of your form submission";
    $message = "Message";
    $message2 = "Message2";
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2);

    if ($_POST['submit']) {
        if ($name != '' && $email != '' && $subject != '' && $message != '') {

            }
        } else {
            echo '<script>function displayPopup()
{
 alert("Form submitted!");
}<script>';
        }
    }

?>

1 个答案:

答案 0 :(得分:1)

因为您只声明了弹出功能,所以您没有运行displayPopup()

尝试更改为

if ($_POST['submit']) {
    if ($name != '' && $email != '' && $subject != '' && $message != '') {

    } else {
        echo '<script>function displayPopup(){alert("Form submitted!");}';
        echo 'displayPopup();</script>
        ';
    }
}