Redirect page after x seconds after filling out sendemail.php form

2 replies · opened Mar 23, 2021

Ffrederic80Mar 23, 2021

Hi,

I've tried to redirect my page after three seconds to a "thank you" page.
I've tried with header and javascript but I don't do it right.
Can you help me with this?
How can I do this?
Could be a new feature/element in your "canvas package".

Here is the page that should be redirected to a thank you page in 3 seconds: https://www.maximgrimonprez.be/contacteer-schrijnwerker-maxim-grimonprez.html

Thanks a lot.
Frederic

Ffrederic80Mar 24, 2021

For your information: this is the PHP code

 'info@maximgrimonprez.be', // Your Email Address
				'name' => 'Maxim Grimonprez' // Your Name
			);

// Form Processing Messages
$message_success = 'We hebben uw bericht **goed ontvangen**. We zullen zo spoedig mogelijk contact met u opnemen.';

// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = ''; // Your reCaptcha Secret

$mail = new PHPMailer();

// If you intend you use SMTP, add your SMTP Code after this Line

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
	if( $_POST['template-contactform-email'] != '' ) {

		$name = isset( $_POST['template-contactform-name'] ) ? $_POST['template-contactform-name'] : '';
		$email = isset( $_POST['template-contactform-email'] ) ? $_POST['template-contactform-email'] : '';
		$phone = isset( $_POST['template-contactform-phone'] ) ? $_POST['template-contactform-phone'] : '';
		$subject = isset( $_POST['template-contactform-subject'] ) ? $_POST['template-contactform-subject'] : '';
		$message = isset( $_POST['template-contactform-message'] ) ? $_POST['template-contactform-message'] : '';

		$subject = isset($subject) ? $subject : 'Nieuw bericht via contactformulier www.maximgrimonprez.be';

		$botcheck = $_POST['template-contactform-botcheck'];

		if( $botcheck == '' ) {

			$mail->SetFrom( $email , $name );
			$mail->AddReplyTo( $email , $name );
			foreach( $toemails as $toemail ) {
				$mail->AddAddress( $toemail['email'] , $toemail['name'] );
			}
			$mail->Subject = $subject;

			$name = isset($name) ? "Naam: $name

" : '';
			$email = isset($email) ? "Email: $email

" : '';
			$phone = isset($phone) ? "GSM: $phone

" : '';
			$message = isset($message) ? "Bericht: $message

" : '';

			$referrer = $_SERVER['HTTP_REFERER'] ? '

Dit contactformulier is ingevuld via: ' . $_SERVER['HTTP_REFERER'] : '';

			$body = "$name $email $phone $message $referrer";

			// Runs only when reCaptcha is present in the Contact Form
			if( isset( $_POST['g-recaptcha-response'] ) ) {
				$recaptcha_response = $_POST['g-recaptcha-response'];
				$response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );

				$g_response = json_decode( $response );

				if ( $g_response->success !== true ) {
					echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
					die;
				}
			}

			// Uncomment the following Lines of Code if you want to Force reCaptcha Validation

			// if( !isset( $_POST['g-recaptcha-response'] ) ) {
			// 	echo '{ "alert": "error", "message": "Captcha not Submitted! Please Try Again." }';
			// 	die;
			// }

			$mail->MsgHTML( $body );
			$sendEmail = $mail->Send();

			if( $sendEmail == true ):
				echo '{ "alert": "success", "message": "' . $message_success . '" }';
			else:
				echo '{ "alert": "error", "message": "Uw bericht kon **niet** verzonden worden door een onverwacht probleem. Probeer later opnieuw.

**Reden:**
' . $mail->ErrorInfo . '" }';
			endif;
		} else {
			echo '{ "alert": "error", "message": "Bot **Detected**.! Clean yourself Botster.!" }';
		}
	} else {
		echo '{ "alert": "error", "message": "Gelieve **alle velden** in te vullen en probeer opnieuw." }';
	}
} else {
	echo '{ "alert": "error", "message": "Een **onverwacht probleem** deed zich voor. Probeer later opnieuw." }';
}

?>
SSemicolon WebSTAFFMar 24, 2021

Hello,

You can simply add the data-redirect="https://your-website.com/thank-you.html" Attribute to the .contact-widget DIV to redirect the Form after successful submission.

Currently, Redirect Delay is not supported out of the box, but you can edit the following code in the js/functions.js File in the contactForm: function() Function Block:
[ch_pre type="js"]if( data.alert != 'error' && elementRedirect ){
window.location.replace( elementRedirect );
return true;
}[/ch_pre]

and replace it with:
[ch_pre type="js"]if( data.alert != 'error' && elementRedirect ){
setTimeout( function(){
window.location.replace( elementRedirect );
}, 3000);
return true;
}[/ch_pre]

This will definitely work fine. Hope this Helps!

We recommend updating to the Latest Versions of Canvas as Support for Older Versions are heavily limited to available features only.

Let us know if we can help you with anything else or if you find any further issues.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard