php表单提交后的HTML网站Javascript警报

时间:2019-03-22 13:22:31

标签: javascript php html

我有HTML网站。 html中有新闻通讯注册表格。它是由php脚本文件处理的,用于发送电子邮件。提交表单后,我希望它返回到原始页面,并且还会显示一条警告消息,“感谢您注册新闻通讯”

HTML:

<div class="col-xs-12 col-sm-12 col-md-6 newsletter-form">
    <form name="contactform" method="post" action="scripts/contact.php" onsubmit="return ValidateForm(contactform)">
        <input type="text" name="stremail" placeholder="Your email address" ><input type="submit" value="Submit">
    </form>
</div>

JavaScript验证:

function ValidateForm(Form)
{
    if (Form.stremail.value == "") {
        alert("Please enter \"Email\" ");
        Form.stremail.focus();
        return (false);
    }
    if ((/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Form.stremail.value)) == false) {
        alert("Invalid E-mail Address! Please re-enter.");
        Form.stremail.focus();
        return (false);
    }

    return (true);
}

PHP脚本:

$stremail = $_POST["stremail"];

$to = "thebrandtgroupre@gmail.com";
$from = $stremail;
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers. = "From: \"$from\" <$from>\r\nReply-To: \"$from\" <$from>\r\nX-Mailer: PHP/".phpversion();
$headers. = "BCC: luxuryproperties@gmail.com,pnparamasivan@gmail.com".
"\r\n"; //for testing purpose 
$subject = "The Brandt Group Newsletter Signup";
$message = "Dear Administrator,\r\n\n";
$message = $message.
"The following information was submitted to the website:<br/><br/>";

$message = $message.
"Email Address  :  ".$stremail.
"<br/><br/>";


mail($to, $subject, $message, $headers);


header("Location: {$_SERVER["
    HTTP_REFERER "]}");
$message2 = "Thank you for newsletter signup";
echo "<script type='text/javascript'>alert('Thank you for newsletter signup');</script>";

有帮助吗?

2 个答案:

答案 0 :(得分:3)

如果您使用的是Jquery,则可以这样做:

HTML端

<div class="col-xs-12 col-sm-12 col-md-6 newsletter-form">
    <form name="contactform" method="post">
        <input type="text" class="stremail" name="stremail" placeholder="Your email address" >
        <input type="submit" value="Submit">
    </form>
</div>

已删除表单操作和onsubmit属性 在stremail输入中添加了一个类

JavaScript

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
    $(function(){
        var form = $('form ');
        form.submit(function(e) {
            e.preventDefault();

            if(ValidateForm(Form)) {
                var data = {
                    'stremail' : $('.stremail').val()
                }
                $.post("scripts/contact.php", data, function(response) {
                    if(response.success) {
                        alert(response.success);
                    }
                    else {
                        // YOUR LOGIC WHEN ERROR OCCURED
                    }
                });
            }
        });

        function ValidateForm(Form) {
            if (Form.stremail.value == "") {
                alert("Please enter \"Email\" ");
                Form.stremail.focus();
                return(false);
            }
            if ((/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Form.stremail.value)) == false) {
                alert("Invalid E-mail Address! Please re-enter.");
                Form.stremail.focus();
                return (false);
            }
            return(true);
        }
    });
</script>

PHP Side

验证$ _POST数据并按类型返回响应

<?php
    $response = [];

    if(isset($_POST["stremail"]) && !empty($_POST["stremail"])) {
        $stremail = $_POST["stremail"];

        $to="thebrandtgroupre@gmail.com";
        $from = $stremail;
        $headers = "Content-type: text/html; charset=iso-8859-1\r\n";
        $headers .="From: \"$from\" <$from>\r\nReply-To: \"$from\" <$from>\r\nX-Mailer: PHP/".phpversion();
        $headers .="BCC: luxuryproperties@gmail.com,pnparamasivan@gmail.com" . "\r\n";//for testing purpose 
        $subject = "The Brandt Group Newsletter Signup";
        $message = "Dear Administrator,\r\n\n";
        $message = $message ."The following information was submitted to the website:<br/><br/>";

        $message = $message ."Email Address  :  ".$stremail."<br/><br/>";


        mail($to,$subject,$message,$headers);

        $response['success'] = "Thank you for newsletter signup";
    }
    else {
        $response['error'] = "YOUR_ERROR_MESSAGE";
    }

    return $response;
?>

答案 1 :(得分:0)

alert不会发生,因为您已经调用header来重定向用户。为了实现javascript弹出窗口的目标,我可能会在发送电子邮件后设置会话变量,然后当您重定向回注册页面(假设它是启用PHP的页面)时,您可以测试该会话并执行JavaScript警报。

<?php

    /* mail handler */
    session_start();

    $stremail = $_POST["stremail"];



    $to="thebrandtgroupre@gmail.com";
    $from = $stremail;
    $headers = "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "From: \"$from\" <$from>\r\nReply-To: \"$from\" <$from>\r\nX-Mailer: PHP/".phpversion();
    $headers .= "BCC: luxuryproperties@gmail.com,pnparamasivan@gmail.com\r\n"; 
    $subject = "The Brandt Group Newsletter Signup";

    $message = "
    Dear Administrator,

        The following information was submitted to the website:
        <br/><br/>
        Email Address:  {$stremail}
        <br /><br />";


    mail( $to, $subject, $message, $headers );


    header( "Location: {$_SERVER["HTTP_REFERER"]}");
    $_SESSION['mail']=true;

    $message = "Thank you for newsletter signup";
?>

注册页面

<?php
    session_start();
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>signup</title>
    </head>
    <body>


        <div class="col-xs-12 col-sm-12 col-md-6 newsletter-form">
            <form name="contactform" method="post" action="scripts/contact.php" onsubmit="return ValidateForm(this)">
                <input type="text" name="stremail" placeholder="Your email address" />
                <input type="submit" />
            </form>
        </div>

        <?php

            if( !empty( $_SESSION['mail'] ) && $_SESSION['mail']==true ){
                printf('<script>alert("%s");</script>', $message );
                unset( $_SESSION['mail'] );
            }

        ?>


    </body>
</html>