-2

I've found a template that I want to edit and it already has a nice looking contact form. After hitting the submit button I'm getting a thank you message but I would like to redirect to another page where the message will appear so I can use it for conversion tracking.

Could someone be of assistance what should I do as my current code looks like this:

<?php


        $firstname = '';
        $number = '';
        $message = '';
        $email = '';


        if($_POST) {
          // collect all input and trim to remove leading and trailing whitespaces
          $firstname = trim($_POST['first_name']);
          $number = trim($_POST['phone']);
          $message = trim($_POST['message']);
          $email = trim($_POST['email']);


          $errors = array();

          // Validate the input
          if (strlen($firstname) == 0)
            array_push($errors, "Please enter your name");

         if (strlen($number) == 0) 
            array_push($errors, "Please specify your number");

          if (strlen($message) == 0) 
            array_push($errors, "Please enter the details of your message");

          if (!filter_var($email, FILTER_VALIDATE_EMAIL))
            array_push($errors, "Please specify a valid email address");


          // If no errors were found, proceed with storing the user input
          if (count($errors) == 0) {

                //completion message into array


                $to  = 'me@mymailzz.com'; // note the comma

                // the email subject ( modify it as you wish )
                $subject = "Enquiry From website";
                // the mail message ( add any additional information if you want )


                $msg = "Quote From website:\n-------------------------------------------\n Name: $firstname \n Number: $number \n Email: $email \n Message: $message \n-------------------------------------------";    
                //function to send email

                    try {
                        mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
                        $return1 = "Thank You for sending your message, we will be in touch within 48-72 hours";


                        array_push($errors, $return1);

                        $formsubmit = 1;

                    }catch(Exception $e){
                        $return3 = "<center><p><b>Your message did not get sent due to an error. Please try again. Caught Exception {$e->getMessage()}</center>";


                        array_push($errors, $return3); 
                        $formsubmit = 0;

                    }

          }

          //Prepare errors for output
          $output = '';
          foreach($errors as $val) {
            $output .= "<p class='output'>$val</p>";
          }



        }
?>
Damir
  • 342
  • 9
  • 21

4 Answers4

2

Replace the line:

$return1 = "Thank You for sending your message, we will be in touch within 48-72 hours";

With something like this:

header('Location: some_page.php');
exit;

And then you can remove these lines as they become obsolete:

array_push($errors, $return1);

$formsubmit = 1;

That will redirect to some_page.php instead of printing that message.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
0

To store the message across pages, do this at the start of your script:

session_start();

Instead of storing messages in a normal variable, store it in a session variable, e.g.:

$_SESSION['message'] = 'Your message here';

At the end of your script:

if ($formsubmit == 1) {
    header('Location: your-file.php');
}

In your-file.php:

<?php
session_start();
echo $_SESSION['message'];
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
0

Just made a change in your try code and insert redirect fuunction

try {
         mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
         echo ("<SCRIPT LANGUAGE='JavaScript'>
            window.location.href='next_page.php'
              </SCRIPT>");
}
Yatin Khullar
  • 1,580
  • 1
  • 12
  • 26
0

How about you trace in your web template where you have <form action="#"> and proceed with help from a previously similar asked question found here

Community
  • 1
  • 1
Joseph
  • 789
  • 1
  • 9
  • 23