skip to Main Content

I am trying to create a contact form that sends an email. I have a simple version on the home page that works and redirects to a thank you page. When I try and run the same PHP script on the contact page it sends the form but does not redirect to the thank you page.

The index.html which send the email and redirects:

<section class="appointment-one" id="booking">
    <div class="container wow fadeInUp"  data-wow-duration="1.5s">
        <div class="inner-container">
            <h3 class="appointment-one__title" >Book a meeting</h3><!-- /.appointment-one__title -->
            <p class="appointment-one__text">Book a free consultation with one of our experts...</p>
            <!-- /.appointment-one__text -->
            <form action="sendmail.php" method="POST" class="appointment-one__form row">
                <div class="col-lg-4">
                    <input type="text" class="miyami_input" placeholder="Name" name="name" required>
                </div><!-- /.col-lg-4 -->
                <div class="col-lg-4">
                    <input type="email" class="miyami_input" placeholder="Email" name="email" required>
                </div><!-- /.col-lg-4 -->
                <div class="col-lg-4">
                    <select name="discussion" class="selectpicker">
                        <option value="">Discussion about</option>
                        <option value="General">General</option>
                        <option value="IT Solutions">IT Solutions</option>
                        <option value="IT Staffing">IT Staffing</option>
                        <option value="IT Outsourcing">IT Outsourcing</option>
                    </select>
                </div><!-- /.col-lg-4 -->
                <div class="col-lg-12">
                    <div class="appointment-one__bottom">
                        <div class="form_button">
                            <button value="Book a meeting" class="miyami_btn g_bg" type="submit">Book a meeting</button>
                        </div>
                    </div><!-- /.appointment-one__bottom -->
                </div><!-- /.col-lg-12 -->
            </form><!-- /.appointment-one__form -->
        </div><!-- /.inner-container -->
    </div><!-- /.container -->
</section><!-- /.appointment-one -->

the sendmail.php file

<?php
$errors = '';
$myemail = '[email protected]';//<-----Put Your email address here.
if(empty($_POST['name']) || empty($_POST['email']))
{
    $errors .= "n Error: all fields are required";
}
$name = $_POST['name'];
$subjects = $_POST['subjects'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$discussion = $_POST['discussion'];
if (!preg_match(
"/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail;
    $email_subject = "Email from VITO";
    $email_body = "You have received a new message. ".
    " Here are the details:n Name: $name".
    "n Discussion: $discussion".
    "n subject: $subjects n ".
    "Email: $email_addressn Message: $message";
    $headers = "From: $myemailn";
    $headers .= "Reply-To: $email_address";
    $mail_status = mail($to,$email_subject,$email_body,$headers);

    if ($mail_status) { ?>
        <script language="javascript" type="text/javascript">
            //   alert('Thank you for the message. We will contact you shortly.');
            window.location.href = 'thankyou.html';
        </script>
        <?php
     }else { ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to [email protected]');
            window.location.href = 'contact.html';
        </script>
    <?php }
//redirect to the 'thank you' page
header('Location: thankyou.html');
}
?>

But when I submit the form on the contact page it doesn’t redirect.

The contact.html page which does not redirect

<div class="contact-info-one__content">
    <div class="container contact-one">
        <div class="block-title text-center">
            <h2 class="block-title__title text-white">Get in touch</h2><!-- /.block-title__title -->
            <p class="block-title__text">Let us help you with your IT requirements.
            </p><!-- /.block-title__text -->
        </div><!-- /.block-title -->
        <form action="sendmail.php" method="POST" class="contact-form-vaidated contact-one__form row">
            <div class="col-lg-6">
                <input type="text" class="miyami_input" placeholder="Name" name="name" required>
            </div><!-- /.col-lg-6 -->
            <div class="col-lg-6">
                <input type="email" class="miyami_input" placeholder="Email" name="email" required>
            </div><!-- /.col-lg-6 -->
            <div class="col-lg-12">
                <input type="text" class="miyami_input" placeholder="Subject" name="subjects" required>
            </div><!-- /.col-lg-6 -->
            <div class="col-lg-12">
                <textarea class="miyami_textarea" placeholder="Message" name="message"></textarea>
            </div><!-- /.col-lg-12 -->
            <div class="col-lg-12 text-center">
                <div class="form_button">
                    <button value="Send" class="thm-btn" type="submit">Send</button>
                </div>
            </div><!-- /.col-lg-12 -->
        </form>

    </div><!-- /.container -->
</div><!-- /.contact-info-one__content -->

Before submitting the form – should be redirected
Before submitting the form - should be redirected

After submitting the form it just clears and doesn’t redirect
After submitting the form it just clears and doesn't redirect

The call to sendmail.php and response
The call to sendmail.php and response

2

Answers


  1. I always include a redirect status code with the location header. I haven’t tried without it, but I know that it does work with it.

    // status depends on http protocol
    if(!$status) {
        $status = strpos($_SERVER['SERVER_PROTOCOL'],'1.1') ? 303 : 302;
    }
    // set redirect headers
    http_response_code($status);
    header("Location: $to");
    

    Any output sent before the redirect will prevent cause header() to fail, so you may want to check first with headers_sent(). This has always been the cause of redirect issues for me in the past.

    Login or Signup to reply.
  2. <?php
    $errors = '';
    $myemail = '[email protected]';//<-----Put Your email address here.
    if(empty($_POST['name']) || empty($_POST['email']))
    {
        $errors .= "n Error: all fields are required";
    }
    $name = $_POST['name'];
    $subjects = $_POST['subjects'];
    $email_address = $_POST['email'];
    $message = $_POST['message'];
    $discussion = $_POST['discussion'];
    if (!preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i",$email_address)) {
        $errors .= "n Error: Invalid email address";
    }
    
    if(empty($errors)) {
        $to = $myemail;
        $email_subject = "Email from VITO";
        $email_body = "You have received a new message. ".
        " Here are the details:n Name: $name".
        "n Discussion: $discussion".
        "n subject: $subjects n ".
        "Email: $email_addressn Message: $message";
        $headers = "From: $myemailn";
        $headers .= "Reply-To: $email_address";
        $mail_status = mail($to,$email_subject,$email_body,$headers);
    
        if ($mail_status) { ?>
            <script language="javascript" type="text/javascript">
                //   alert('Thank you for the message. We will contact you shortly.');
                window.location.href = 'thankyou.html';
            </script>
            <?php
        }else { ?>
            <script language="javascript" type="text/javascript">
                alert('Message failed. Please, send an email to [email protected]');
                window.location.href = 'contact.html';
            </script>
        <?php }
    }
    else {
        ?>
        <script language="javascript" type="text/javascript">
                alert('Errors: <?php echo $errors;?>');
            </script>
        <?php
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search