0

I'm new to PHP and am using a script to send emails when a user makes use of a websites contact us form. The emails are working fine. The problem I have however is that when a user submits a message they are redirected to a new page stating "Contact form successfully submitted. Thank you, I will get back to you soon!". What I would like is a simple message on the same page above the form (i.e. green message saying something like "email sent".

The PHP script is:

Code Share

My HTML form currently looks like:

                <form id="contact-form" method="post" action="sendemail.php" role="form">

                <div class="messages"></div>

                <div class="controls">

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="form_name">Firstname *</label>
                                <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="form_lastname">Lastname *</label>
                                <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="form_email">Email *</label>
                                <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="form_phone">Phone</label>
                                <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your phone">
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="form_message">Message *</label>
                                <textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="6" required="required" data-error="Please,leave us a message."></textarea>
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <input type="submit" class="btn btn-success btn-send" value="Send message">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <p class="text-muted"><strong>*</strong> These fields are required.</p>
                        </div>
                    </div>
                </div>

            </form>

Thanks in advance!

2 Answers2

0

The best way would be to make a script in PHP to send your emails as you already do (you may need to modify it). You will need to change your form to use Ajax. This will allow you to send the POST data to the script which will send the email but it will also give you a response and on that responses you can then display your sent message.

Sean
  • 1,444
  • 1
  • 11
  • 21
  • Thanks. Any ideas how I can make the change based on the code that I have provided. I'm worried about amending the PHP script as I have tried a number of changes that stopped it working completely! –  Aug 07 '17 at 11:50
  • Are you using jquery on your frontend? – Sean Aug 07 '17 at 11:55
  • Hi, yes I am :) –  Aug 07 '17 at 12:01
0

Ok so after talking in the comments I have created this:

<div class="messages"></div>

<div class="controls">

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="form_name">Firstname *</label>
                <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="form_lastname">Lastname *</label>
                <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
                <div class="help-block with-errors"></div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="form_email">Email *</label>
                <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="form_phone">Phone</label>
                <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your phone">
                <div class="help-block with-errors"></div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <label for="form_message">Message *</label>
                <textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="6" required="required" data-error="Please,leave us a message."></textarea>
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="col-md-12">
            <button class="btn btn-success btn-send" id="sendEmailBtn">Send message</button>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <p class="text-muted"><strong>*</strong> These fields are required.</p>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#sendEmailBtn").click(function () {
            /* Disable the input fields and button so the user
                can't edit them or submit the form multiple times */
            $("input").prop("disabled", true);
            $("#sendEmailBtn").prop("disabled", true);

            // Submit the form via ajax
            $.post("http://yourdomain.com/sendemail.php", {
                name: $("#form_name").val(),
                surname: $("#form_lastname").val(),
                email: $("#form_email").val(),
                phone: $("#form_phone").val(),
                message: $("#form_message").val()
            }, function (data, status) {
                /* Responses, check the data is correct if it is display
                   your message else enable the form elements and button
                   and display an error message */
                console.log(data, status);
                // Method to display your message, maybe something like this: $(".messages").html("<h1>your messages</h1>");
            });
        });
    });
</script>

This should work without your PHP script needing to be changed.

Sean
  • 1,444
  • 1
  • 11
  • 21
  • Hi y0hami. I'm afraid I'm still being redirected to a new page with the message "Contact form successfully submitted. Thank you, I will get back to you soon!" I think this is due to something in the php script causing a page redirect... –  Aug 07 '17 at 12:49
  • Did you change your HTML to the one above? – Sean Aug 07 '17 at 12:49
  • Hi. I've got a bit further now (I original placed the script in a separate .js file and linked to it) but have now placed the script directly into the HTML page. What I am finding now is that the message gets sent but when the user clicks submit the button becomes unclickable (as expected) but no message or notification appears saying the mail had been sent. –  Aug 07 '17 at 12:56
  • 1
    If you open your console (if using Chrome click F12) you will see a message will appear once the ajax request is complete. This message will be the body of the script page. You should return some JSON on your script page to indicate your request was successful or failed. This will help you when displaying the message. Also the message didn't appear because I didn't add it I left that up to you, look at the comments in the script. – Sean Aug 07 '17 at 13:03
  • Sorry, I'm a complete doughnut and didn't read the script part that stated the recommended message part; my bad! It's working now. Thanks so much for your help! –  Aug 07 '17 at 13:09
  • I hope you don't mind but I have one further question. If I wanted to change the style of the message to say the color of green - then how can I do this? I've tried using the associated class (.message) but am unable to get it to change. I then tried to add a class to the $(".messages").html(" –  Aug 07 '17 at 13:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151278/discussion-between-y0hami-and-bs3ac). – Sean Aug 07 '17 at 13:31