0

Here is my code which I am using in my website. I have a single page website. As I click my submit button, my whole page gets refreshed.

Please help me out with this issue. For refrence, visit http://www.creativemacky.com

<?php
    $to      = 'my email';
    $email   = $_POST['email'];

    $name  = $_POST['name'];
    $message = $_POST['text-massage'];

    $headers = 'From: http://www.creativemacky.com'. '<'.$email.'>' . "\r\n" .
        'Reply-To: '. $email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    if (mail($to, $subject, $message, $headers)){
     echo "<script>window.location.href = 'http://www.creativemacky.com';</script>";
    }

?>

<form class="_form-inline row" id="contact-form" name="contact" method="post" action="contact.php">
                                    <div class="form-group custom-form-group col-sm-6">
                                        <label class="sr-only" for="name" id="name"><span class="required">*</span>Name</label>
                                        <input type="text" class="form-control custom-form-control wow bounceInLeft animated" name="name" id="name" placeholder="Enter your name" required>
                                    </div>

                                    <div class="form-group custom-form-group col-sm-6">
                                        <label class="sr-only" for="email" id="email"><span class="required">*</span>Email</label>
                                        <input type="email" class="form-control custom-form-control wow bounceInRight animated" id="email" name="email" placeholder="Enter your email" required>
                                    </div>
                                    <div class="form-group custom-form-group col-sm-12">
                                        <label class="sr-only" for="text-massage"><span class="required">*</span>Massage</label>
                                        <textarea  class="form-control custom-form-control wow bounceInUp animated" id="text-massage" name="text-massage" rows="3" placeholder="Write something" required></textarea>
                                    </div>

                                    <button type="submit" class="pull-right btn btn-default submit-button custom-white wow bounceInLeft animated" id="submit" name="submit" value="Send">Say Hello</button>
                                </form>

2 Answers2

0

You will need to split the php from the page in order to submit without refreshing php ajax form submit this should help you with submitting without refreshing the page.

Community
  • 1
  • 1
jagler
  • 185
  • 1
  • 6
0

Normally to post form data to a server without refreshing you need to use JavaScript. In JavaScript you catch the "submit" event and use AJAX to send the data to the server. This is easy with jQuery.

<script>

    $( "#contact-form" ).submit(function( event ) {
        var data = {
            name: $('#name').val(),
            email: $('#email').val(),
            textMessage: $('#text-message').val()
        }
        $.post('contact.php', data);
        event.preventDefault();
    });
</script>
nano2nd
  • 333
  • 3
  • 10