发送表单后显示成功消息的困难 [英] Difficulties with displaying succes message after form is sent

查看:60
本文介绍了发送表单后显示成功消息的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习用于创建表单的代码语言。所以我似乎无法弄清楚似乎是什么问题。这是我的代码:



HTML:



I'm still learning the code languages that I used to create my form with. So I can't seem to figure out what seems to be the problem. Here's my code:

HTML:

<form action="" method="POST">
    <ul class="form-style-1">
        <li>
            <input type="text" id="mail-name" name="name" class="field-divided" maxlength="15"  placeholder="Voornaam *" /> <input type="text" id="mail-lastname" name="lastname" class="field-divided" maxlength="15" placeholder="Achternaam" >
        </li>
        <li>
            <input type="email" id="mail-email" name="email" placeholder="E-mail *" class="field-long" maxlength="40" >
        </li>
        <li>
            <input type ="text" id="mail-phone" name="phone" placeholder="Telefoonnummer" class="field-long" maxlength = "15">
        </li>
        <li>
            <select name="subject" id="mail-subject" class="field-select" >
            <option disabled value="" selected hidden >--Onderwerp-- *</option>
            <option value="Kennismakingsgesprek">Kennismakingsgesprek</option>
            <option value="Meer informatie">Meer informatie</option>
            <option value="activiteit">Aanmelding activiteit</option>
            <option value="Vraag/klacht">Vraag/klacht</option>
            <option value="Contact">Overig</option>
            </select>
        </li>
        <li>
            <textarea name="information" id="mail-information"  placeholder =" Je bericht *"class="field-long field-textarea" maxlength="2000"></textarea>
        </li>
        <button class="mail-submit" id="mail-submit" type="submit" name="submit">Send e-mail</button>
        <span class="form-message"></span>
    </ul>
</form>





JS:





JS:

<script>
   $("#mail-submit").click(function(event){
    event.preventDefault();
    var name = $("#mail-name").val();
    var lastname = $("#mail-lastname").val();
    var email = $("#mail-email").val();
    var phone = $("#mail-phone").val();
    var subject = $("#mail-subject").val();
    var information = $("#mail-information").val();
    $.post("contacttest.php",
        {
          name: name,
          lastname: lastname,
          email: email,
          phone: phone,
          subject: subject,
          information: information,
          submit: "yes" 
        },
        function(data){
            $(".form-message").html( data );
        }
    );
});   





PHP:



PHP:

<?php

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


    $email_to = "#";

    $email_subject = "#";

    $name = $_POST['name'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $subject = $_POST['subject'];
    $information = $_POST['information'];


    $errorEmpty = false;
    $errorEmail = false;

    if (empty($name) || empty($email) || empty($subject) || empty($information)) {
        echo "<span class='form-error'>Voer alle velden in!</span>";
        $errorEmpty = true;
    }
    elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "<span class='form-error'>Geef een geldig E-mail!</span>";
        $errorEmail = true; 
    }
    else {
        $formcontent=" Naam: $name \n\n Achternaam: $lastname \n\n Email: $email \n\n Telefoon: $phone \n\n Onderwerp: $subject \n\n Informatie: $information";
        $mailheader = "From: ".$_POST["email"]."\r\n"; 
        $headers = "From: ". htmlspecialchars($_POST['name']) ." <" . $_POST['email'] . ">\r\n";
        $headers .= "Reply-To: " . $_POST['email'] . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        mail($email_to, $subject, $formcontent, $mailheader);
        echo "<span class='form-success'>E-mail has been sent!</span>";
    }

}
else {
    echo "Not working!";
}

?>





我正在寻找的是该页面没有表单发送后不刷新,以便它可以显示表单成功或错误消息。目前,页面没有刷新(我认为),但它在发送表单时拒绝显示成功消息。所有代码都位于1个PHP文件中。



感谢您的时间。



< b>我尝试了什么:



基本上通过互联网了解这个问题,并没有解决我的问题。



What I'm looking for is that the page doesn't refresh after the form has been sent, so that it can display the form-succes or error message. At the moment, the page doesn't refresh(I think), but it refuses to show the succes message when the form is sent. All of the code are located in 1 PHP file.

Thank you for your time.

What I have tried:

Basically looked through the internet about this problem, didn't fix my problem.

推荐答案

(#mail-submit)。click(function(event){
event.preventDefault();
var name =
("#mail-submit").click(function(event){ event.preventDefault(); var name =


(#mail-name)。val();
var lastname =
("#mail-name").val(); var lastname =


(#mail-lastname)。val();
var email =
("#mail-lastname").val(); var email =


这篇关于发送表单后显示成功消息的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆