简体   繁体   English

提交联系表后显示“谢谢”消息

[英]Showing a “thank you” message after submiting a Contact Form

I have a working Contact Form on my website.我的网站上有一个有效的联系表。 The submit button leads to a.php file that is sending an e-mail (User doesn't see this step) and redirect user to contact form again.提交按钮指向一个正在发送电子邮件的.php 文件(用户看不到此步骤),并将用户再次重定向到联系表单。

I wanna make a simple message on the center of the screen after clicking the submit button.单击提交按钮后,我想在屏幕中央发出一条简单的消息。 Something like "Thank you for sending the application".诸如“感谢您发送申请”之类的内容。

I tried to set a variable in the php file我试图在 php 文件中设置一个变量

$mailSent = 1;

and then catch it in the script然后在脚本中捕获它

var mailSent = "<?php echo $mailSent; ?>";

Then I wanted to do a simple if statement and style.display the message cotnainer.然后我想做一个简单的 if 语句和样式。显示消息 cotnainer。 Sadly it didn't work out, I was getting the undefined errors.可悲的是它没有成功,我得到了未定义的错误。

if (mailSent == 1) {
    document.querySelector('.thankYouMessage').style.display = 'flex';
}

I wanted to ask you if you have any ideas on how to do it.我想问你是否对如何做有任何想法。 I want the message to look like a bootstrap modal.我希望消息看起来像引导模式。

This is my contact form:这是我的联系表格:

<div class="contact-form-container">
            <div class="contact-form">
                <form action="contact-form-handler.php" method="POST">
                    <h1 class="lang" key="contact-form" style="margin: 0 auto; font-size: 22px; font-family: Open Sans; color: #ff4800; margin-bottom: 10px; padding-bottom: 4px; font-weight: 100; width: 100%; text-align: center;">Formularz kontaktowy</h1>

                    <label class="lang" key="contact-form-name" for=""></label>
                    <input id="name" class="contact-form-content" type="text" name="name" required>
                    
                    <label class="lang" key="contact-form-email" for=""></label>
                    <input id="email" class="contact-form-content" type="email" name="email" required>
                    
                    <label class="lang" key="contact-form-topic" for=""></label>
                    <input id="topic" class="contact-form-content" type="text" name="topic" required>
                    
                    <label class="lang" key="contact-form-text" for=""></label>
                    <textarea id="message" name="message" style="line-height:16px; font-size: 14px; font-family: calibri;" class="contact-form-content" cols="" rows="10"></textarea>
                    
                    <button class="contact-form-content-submit" name="submit" type="submit"><p class="lang" key="contact-form-send" style="margin: 0; padding: 0;">Wyślij</p><img id ="Contact_icon_white" src="img/Contact_icon_white.png" alt="White contact icon"></button>  
                </form>
                <div class="contact-form-callnow" style="display: flex; align-items: center; justify-content: center; margin: 0; padding: 0;">
                    <p class="lang" key="callnow" id="contact-form-callnow" style="font-family: Open Sans; margin: 0; padding: 14px 0px 4px 0px;">Zadzwoń teraz i umów przeprowadzkę!</p>
                </div>
                <div style="display: flex; align-items: center; justify-content: center; margin: 0; padding: 0;">
                    <a style="margin: 0; padding: 0;" href="tel:+48732739751"><img src="img/Phone_icon_green.png" alt="Phone icon" style="height: 28px; padding-right: 10px"></a>
                    <a style="margin: 0; padding: 0;" href="tel:+48732739751"><p style="font-family: Open Sans; margin: 0; padding: 4px 0px 10px 0px; font-size: 20px;">732 739 751</p></a>
                </div>
            </div>
        </div>

And this is my php Contact-form-handler这是我的 php Contact-form-handler

<?php

if(isset($_POST['submit'])) {

    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $topic = $_POST['topic'];
    $message = $_POST['message'];

    $to = "xyz@xyz.pl";

    $subject = "Tu ".$name.". Wiadomość do xyz";

    $headers = "From: ".$visitor_email;

    $txt = "\nNowa wiadomość od: ".$name."\n\n\nTemat:\n".$topic."\n\n\nWiadomość:\n".$message;

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

    header ('Location: contact.php');  

} else {

    header ('location: index.php');

}

?> ?>

You are trying to add an already parsed php code.您正在尝试添加已解析的 php 代码。 PHP code is parsed to HTML code first, and you are adding the PHP code again to your Javascript code. PHP 代码首先被解析为 HTML 代码,然后您将再次将 PHP 代码添加到您的 Z9E13B69D1D2DAACAAAF771 代码中。

Add this:添加这个:

$mailSent = false;

If your mail is successfully sent, switch that variable to true .如果您的邮件发送成功,请将该变量切换为true Then:然后:

<?php
if($mailSent === true) {
?>
    <div class="success"></div>
<?php
}
?>

Edit: Try This:编辑:试试这个:

<?php

$mailSent = false;

if(isset($_POST['submit'])) {

        $name = $_POST['name'];
        $visitor_email = $_POST['email'];
        $topic = $_POST['topic'];
        $message = $_POST['message'];

        $to = "marcincelmer95@gmail.com";

        $subject = "Tu ".$name.". Wiadomość do poznanprzeprowadzki.pl";

        $headers = "From: ".$visitor_email;

        $txt = "\nNowa wiadomość od: ".$name."\n\n\nTemat:\n".$topic."\n\n\nWiadomość:\n".$message;

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

        $mailSent = true;

}

?>

<?php
if ($mailSent === true) {
?>
<div class="success">Mail is succesfully sent.</div>
<?php
}
?>

<div class="contact-form-container">
            <div class="contact-form">
                <form action="contact.php" method="POST">
                    <h1 class="lang" key="contact-form" style="margin: 0 auto; font-size: 22px; font-family: Open Sans; color: #ff4800; margin-bottom: 10px; padding-bottom: 4px; font-weight: 100; width: 100%; text-align: center;">Formularz kontaktowy</h1>

                    <label class="lang" key="contact-form-name" for=""></label>
                    <input id="name" class="contact-form-content" type="text" name="name" required>
                    
                    <label class="lang" key="contact-form-email" for=""></label>
                    <input id="email" class="contact-form-content" type="email" name="email" required>
                    
                    <label class="lang" key="contact-form-topic" for=""></label>
                    <input id="topic" class="contact-form-content" type="text" name="topic" required>
                    
                    <label class="lang" key="contact-form-text" for=""></label>
                    <textarea id="message" name="message" style="line-height:16px; font-size: 14px; font-family: calibri;" class="contact-form-content" cols="" rows="10"></textarea>
                    
                    <button class="contact-form-content-submit" name="submit" type="submit"><p class="lang" key="contact-form-send" style="margin: 0; padding: 0;">Wyślij</p><img id ="Contact_icon_white" src="img/Contact_icon_white.png" alt="White contact icon"></button>  
                </form>
                <div class="contact-form-callnow" style="display: flex; align-items: center; justify-content: center; margin: 0; padding: 0;">
                    <p class="lang" key="callnow" id="contact-form-callnow" style="font-family: Open Sans; margin: 0; padding: 14px 0px 4px 0px;">Zadzwoń teraz i umów przeprowadzkę!</p>
                </div>
                <div style="display: flex; align-items: center; justify-content: center; margin: 0; padding: 0;">
                    <a style="margin: 0; padding: 0;" href="tel:+48732739751"><img src="img/Phone_icon_green.png" alt="Phone icon" style="height: 28px; padding-right: 10px"></a>
                    <a style="margin: 0; padding: 0;" href="tel:+48732739751"><p style="font-family: Open Sans; margin: 0; padding: 4px 0px 10px 0px; font-size: 20px;">732 739 751</p></a>
                </div>
            </div>
        </div>

Now it looks like this:现在看起来像这样:

<?php

$mailSent = 0;

echo $mailSent;

if ($mailSent === 1) {
    
    echo "<script>

        function myFunction() {
            document.querySelector('.modal-background').style.display = 'flex';
        };

    </script>";

}

else if ($mailSent === 0) {

    if(isset($_POST['submit'])) {

        $name = $_POST['name'];
        $visitor_email = $_POST['email'];
        $topic = $_POST['topic'];
        $message = $_POST['message'];

        $to = "marcincelmer95@gmail.com";

        $subject = "Tu ".$name.". Wiadomość do poznanprzeprowadzki.pl";

        $headers = "From: ".$visitor_email;

        $txt = "\nNowa wiadomość od: ".$name."\n\n\nTemat:\n".$topic."\n\n\nWiadomość:\n".$message;

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

        $mailSent = 1;  

    } 

}

else {

}

?> ?>

        <div class="contact-form-container">
            <div class="contact-form">
                <form action="contact.php" method="POST">
                    <h1 class="lang" key="contact-form" style="margin: 0 auto; font-size: 22px; font-family: Open Sans; color: #ff4800; margin-bottom: 10px; padding-bottom: 4px; font-weight: 100; width: 100%; text-align: center;">Formularz kontaktowy</h1>

                    <label class="lang" key="contact-form-name" for=""></label>
                    <input id="name" class="contact-form-content" type="text" name="name" required>
                    
                    <label class="lang" key="contact-form-email" for=""></label>
                    <input id="email" class="contact-form-content" type="email" name="email" required>
                    
                    <label class="lang" key="contact-form-topic" for=""></label>
                    <input id="topic" class="contact-form-content" type="text" name="topic" required>
                    
                    <label class="lang" key="contact-form-text" for=""></label>
                    <textarea id="message" name="message" style="line-height:16px; font-size: 14px; font-family: calibri;" class="contact-form-content" cols="" rows="10"></textarea>
                    
                    <button class="contact-form-content-submit" name="submit" type="submit"><p class="lang" key="contact-form-send" style="margin: 0; padding: 0;">Wyślij</p><img id ="Contact_icon_white" src="img/Contact_icon_white.png" alt="White contact icon"></button>  
                </form>
                <div class="contact-form-callnow" style="display: flex; align-items: center; justify-content: center; margin: 0; padding: 0;">
                    <p class="lang" key="callnow" id="contact-form-callnow" style="font-family: Open Sans; margin: 0; padding: 14px 0px 4px 0px;">Zadzwoń teraz i umów przeprowadzkę!</p>
                </div>
                <div style="display: flex; align-items: center; justify-content: center; margin: 0; padding: 0;">
                    <a style="margin: 0; padding: 0;" href="tel:+48732739751"><img src="img/Phone_icon_green.png" alt="Phone icon" style="height: 28px; padding-right: 10px"></a>
                    <a style="margin: 0; padding: 0;" href="tel:+48732739751"><p style="font-family: Open Sans; margin: 0; padding: 4px 0px 10px 0px; font-size: 20px;">732 739 751</p></a>
                </div>
            </div>
        </div>

The program echo's the $mailSent = 0, but despite the fact that it sends an email correctly, it does not overwrite the $mailSent variable...程序回显 $mailSent = 0,但尽管它正确发送了 email,但它并没有覆盖 $mailSent 变量...

I'm a beggining programmer so I see this bugs for the first time in my life, sorry for being ignorant in some cases.我是一个初学者,所以我有生以来第一次看到这个错误,很抱歉在某些情况下无知。 I would love to learn frameworks but I feel like it's a bit too early for me to understand it all.我很想学习框架,但我觉得现在理解这一切还为时过早。

This is my website's URL if it helps somehow:这是我网站的 URL 如果它有帮助的话:

www.poznanprzeprowadzki.pl www.poznanprzeprowadzki.pl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM