Skip to content Skip to sidebar Skip to footer

Form Message Alert

I have the following form:
&l

Solution 1:

HTML Code --

<pstyle="display:none">Mail Send Successfully </p><formmethod="post"action="contactus.php"name="myForm"autocomplete="off"><labelfor="Name">Name:</label><inputtype="text"name="Name"id="Name"maxlength="60"required/><labelfor="email">Email:</label><inputtype="text"name="email"id="email"maxlength="120"required/><labelfor="message">Message:</label><br /><textareaname="message"rows="20"cols="20"id="message"required></textarea><inputid="submit-btn"type="submit"name="submit"value="Submit"class="submit-button" /></form>

AJAX Code --

<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script><scripttype="text/javascript">
        $(document).ready(function(){
            $('#submit-btn').click(function(){
                var name = $('#name').val();
                var email = $('#email').val();
                var message = $('#message').val();

                $.ajax({
                type: "POST",
                url: "form-submit.php", // in this file we will put the code of send email and also we will return a responsedata:"name="+name+"&email="+email+"&message="+message,
                success: function(response) // we will get response
                    {
                        $('p').show(); // here message will show after send email
                    }
              }); 
        });
        });
        </script>

Solution 2:

If the PHP that sends mail is on the same page, I'm assuming a structure like this:

<?phpif (isset($_POST['email']) && strlen($_POST['email') {
    mail("name@example.com", "subject", "text");
}
?>

I recommend you add a variable there, like this:

<?phpif (isset($_POST['email']) && strlen($_POST['email') {
    mail("name@example.com", "subject", "text");
    $sent_mail = true; // so we know later
}
?>

Then somewhere in the page, do this:

<?phpif (isset($sent_mail)) {
    echo'Success!';
}
?>

No javascript needed :)

Solution 3:

<formmethod="post"action="contactus.php?message=ok"name="myForm"autocomplete="off"><labelfor="Name">Name:</label><inputtype="text"name="Name"id="Name"maxlength="60"required/><labelfor="email">Email:</label><inputtype="text"name="email"id="email"maxlength="120"required/><labelfor="message">Message:</label><br /><textareaname="message"rows="20"cols="20"id="message"required</textarea><inputtype="submit"name="submit"value="Submit"class="submit-button" /></form><divid="displayMessage"></div>*****************new div to display message put wherever you want******************

Javascript:

<script>functiongetParameterByName(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = newRegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
        if(getParameterByName("message") == "ok")
        {
            document.getElementById("displayMessage").innerHTML = "Message Sent.";
            window.alert("Message Sent.");//If you want an alert box
        }
<script>

Just put the <script></script> at the end of your body...

Post a Comment for "Form Message Alert"