-1

I have checked How do I redirect to another webpage? and though they do successfully redirect, they remove the echo 'OK' function I have implemented.

I have a form and would like to redirect back to the home page 5 seconds after the form has been submitted.

However, there needs to be an echo first that says "OK" which links to a jQuery script and shows a green box that says "Your message has been sent (<div class="sent-message">). Thank you!". I would like to display that for 5 seconds, before redirecting back to the home page.

I know you cannot use header("Location: http://www.yourwebsite.com/index.html"); after echo but is there another way to do this?

This is what the form looks like when it is successfully submitted. The green box is what echo 'OK'; does. https://ibb.co/BjZ9v47

contact.php

    <?php
    
    // script to send myself an email with the data entered into the form by a user

    if($mail->Send()) {
       echo 'OK';
       // redirect to index.html after 5 seconds
    }
            
    ?>

form.html

      <form action="contact.php" method="post" role="form" class="php-email-form">
        <div class="form-group">
          <input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
          <div class="validate"></div>
        </div>
        <div class="form-group">
          <input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
          <div class="validate"></div>
        </div>
        <div class="form-group">
          <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
          <div class="validate"></div>
        </div>
        <div class="form-group">
          <textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
          <div class="validate"></div>
        </div>
        
        <div class="captcha">
          <div class="g-recaptcha" data-sitekey="my_site_key" data-callback="removeFakeCaptcha"></div>
          <input type="checkbox" class="captcha-fake-field" tabindex="-1" required>
        </div>

        <div class="mb-3">
          <div class="loading">Loading</div>
          <div class="error-message"></div>
          <div class="sent-message">Your message has been sent. Thank you!</div>
        </div>

        <div class="text-center"><button type="submit" title="Send Message">Send Message</button></div>
      </form>

jQuery script:

!(function($) {
  "use strict";

  $('form.php-email-form').submit(function(e) {
    e.preventDefault();
    
    var f = $(this).find('.form-group'),
      ferror = false,
      emailExp = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;

    f.children('input').each(function() { // run all inputs
     
      var i = $(this); // current input
      var rule = i.attr('data-rule');

      if (rule !== undefined) {
        var ierror = false; // error flag for current input
        var pos = rule.indexOf(':', 0);
        if (pos >= 0) {
          var exp = rule.substr(pos + 1, rule.length);
          rule = rule.substr(0, pos);
        } else {
          rule = rule.substr(pos + 1, rule.length);
        }

        switch (rule) {
          case 'required':
            if (i.val() === '') {
              ferror = ierror = true;
            }
            break;

          case 'minlen':
            if (i.val().length < parseInt(exp)) {
              ferror = ierror = true;
            }
            break;

          case 'email':
            if (!emailExp.test(i.val())) {
              ferror = ierror = true;
            }
            break;

          case 'checked':
            if (! i.is(':checked')) {
              ferror = ierror = true;
            }
            break;

          case 'regexp':
            exp = new RegExp(exp);
            if (!exp.test(i.val())) {
              ferror = ierror = true;
            }
            break;
        }
        i.next('.validate').html((ierror ? (i.attr('data-msg') !== undefined ? i.attr('data-msg') : 'wrong Input') : '')).show('blind');
      }
    });
    f.children('textarea').each(function() { // run all inputs

      var i = $(this); // current input
      var rule = i.attr('data-rule');

      if (rule !== undefined) {
        var ierror = false; // error flag for current input
        var pos = rule.indexOf(':', 0);
        if (pos >= 0) {
          var exp = rule.substr(pos + 1, rule.length);
          rule = rule.substr(0, pos);
        } else {
          rule = rule.substr(pos + 1, rule.length);
        }

        switch (rule) {
          case 'required':
            if (i.val() === '') {
              ferror = ierror = true;
            }
            break;

          case 'minlen':
            if (i.val().length < parseInt(exp)) {
              ferror = ierror = true;
            }
            break;
        }
        i.next('.validate').html((ierror ? (i.attr('data-msg') != undefined ? i.attr('data-msg') : 'wrong Input') : '')).show('blind');
      }
    });
    if (ferror) return false;

    var this_form = $(this);
    var action = $(this).attr('action');

    if( ! action ) {
      this_form.find('.loading').slideUp();
      this_form.find('.error-message').slideDown().html('The form action property is not set!');
      return false;
    }
    
    this_form.find('.sent-message').slideUp();
    this_form.find('.error-message').slideUp();
    this_form.find('.loading').slideDown();

    if ( $(this).data('recaptcha-site-key') ) {
      var recaptcha_site_key = $(this).data('recaptcha-site-key');
      grecaptcha.ready(function() {
        grecaptcha.execute(recaptcha_site_key, {action: 'php_email_form_submit'}).then(function(token) {
          php_email_form_submit(this_form,action,this_form.serialize() + '&recaptcha-response=' + token);
        });
      });
    } else {
      php_email_form_submit(this_form,action,this_form.serialize());
    }
    
    return true;
  });

  function php_email_form_submit(this_form, action, data) {
    $.ajax({
      type: "POST",
      url: action,
      data: data,
      timeout: 40000
    }).done( function(msg){
      if (msg == 'OK') {
        this_form.find('.loading').slideUp();
        this_form.find('.sent-message').slideDown();
        this_form.find("input:not(input[type=submit]), textarea").val('');
      } else {
        this_form.find('.loading').slideUp();
        if(!msg) {
          msg = 'Form submission failed and no error message returned from: ' + action + '<br>';
        }
        this_form.find('.error-message').slideDown().html(msg);
      }
    }).fail( function(data){
      console.log(data);
      var error_msg = "Form submission failed!<br>";
      if(data.statusText || data.status) {
        error_msg += 'Status:';
        if(data.statusText) {
          error_msg += ' ' + data.statusText;
        }
        if(data.status) {
          error_msg += ' ' + data.status;
        }
        error_msg += '<br>';
      }
      if(data.responseText) {
        error_msg += data.responseText;
      }
      this_form.find('.loading').slideUp();
      this_form.find('.error-message').slideDown().html(error_msg);
    });
  }

})(jQuery);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • You could look at output buffering - search `ob_start()`. There's probably a better way to arrange things though. – droopsnoot Oct 06 '20 at 12:37
  • @droopsnoot - Using ob_start() won't make the client wait before redirecting. I don't see how output buffering would help in this context. – M. Eriksson Oct 06 '20 at 12:43
  • Don't know why but your question is marked as Duplicate although you have mentioned that you need a redirect after 5 secs. Your question title should be something like this: "Redirect to a page after successful form submission after 5 secs" – Manish Pareek Oct 06 '20 at 13:49
  • 1
    I can't see how the duplicates and answers given do not work for you here. Enable error reporting and look at your developer console. – Funk Forty Niner Oct 06 '20 at 13:58
  • I have done this! Nothing appears in the console. For some reason, anything after `this_form.find("input:not(input[type=submit]), textarea").val('');` in the jQuery file does not seem to register. @FunkFortyNiner – slkdanfalvsd Oct 06 '20 at 14:00

2 Answers2

1

You can echo a <meta> refresh with a set URL, although this isn't the best approach.

if (!$mail->Send()) {
  echo "Mail sending failed";
}
else {
  echo "Mail sent!";
  echo "<meta http-equiv='refresh' content='5;URL=/index.html'>";
}

More information about the meta tag here.

Reece
  • 574
  • 3
  • 10
  • Are you able to make it wait 3 seconds before redirecting so the user can see the "Mail Sent"? @Reece – slkdanfalvsd Oct 06 '20 at 12:40
  • The issue now is that if I use this, it simply displays a red box that says OK instead of calling `
    ` @Reece
    – slkdanfalvsd Oct 06 '20 at 12:49
  • In that case, it may be worth looking into the answer that [Ivar](https://stackoverflow.com/users/479156/ivar) suggested [here](https://stackoverflow.com/questions/6119451/page-redirect-after-certain-time-php/6119499#6119499), instead of using the `meta` refresh in my answer. – Reece Oct 06 '20 at 12:51
  • You cannot use `header` after an `echo` unfortunately @Reece – slkdanfalvsd Oct 06 '20 at 12:53
  • @slkdanfalvsd - They aren't. They are outputting `` tags in the HTML. – M. Eriksson Oct 06 '20 at 19:21
1

In your $.ajax done callback, inside if you can use setTimeout:

if (msg == 'OK') {
  this_form.find('.loading').slideUp();
  this_form.find('.sent-message').slideDown();
  this_form.find("input:not(input[type=submit]), textarea").val('');
  //redirect to specific url after 5secs
  window.setTimeout(function () {
    window.location.href = 'https://www.example.com'; //your home url here
  }, 5000);
}
Manish Pareek
  • 409
  • 5
  • 12
  • I tried adding this but it does not do anything... @mPareek – slkdanfalvsd Oct 06 '20 at 13:18
  • Also, the last `}` should not be there as there is an `else` below that – slkdanfalvsd Oct 06 '20 at 13:19
  • I have given an example by using only `if` statement. Check the errors in your browser console. – Manish Pareek Oct 06 '20 at 13:21
  • There are none. After I press submit, it shows the green box and sends the email. Nothing happens after 5 seconds of waiting. @mPareek – slkdanfalvsd Oct 06 '20 at 13:22
  • Put `console.log('5 secs are completed')` inside `window.setTimeout(function () {` and check if you get any console message after 5 secs – Manish Pareek Oct 06 '20 at 13:26
  • Still no log This is what it looks like now: if (msg == 'OK') { this_form.find('.loading').slideUp(); this_form.find('.sent-message').slideDown(); this_form.find("input:not(input[type=submit]), textarea").val(''); window.setTimeout(function () { console.log('5 secs are completed'); window.location.href = 'https://www.mywebsite.com/'; //your home url here }, 5000); – slkdanfalvsd Oct 06 '20 at 13:32
  • Does your form is submitting properly and are you getting the success(or whatever message you have put) message after form submission? – Manish Pareek Oct 06 '20 at 13:35
  • Yes, when I submit I get the green box with a successful message. Also, the email goes through @mPareek – slkdanfalvsd Oct 06 '20 at 13:36