Skip to main content
higgsyd
Inspiring
May 8, 2021
Answered

phpmailer runs ok but thankyou page is not displayed

  • May 8, 2021
  • 2 replies
  • 1141 views

On a previous post I was recommended by your good selves to stop using an old Perl script and use instead phpmailer.  This I have now implemented BUT…

 

After my contact form has been completed and submitted I want the thank you page to be displayed. Although the email is sent successfully the thank you page is not displayed.  I got the code off the Internet and I do largely understand how it works:

 

 the contact_form.php displays the form and when Submit is clicked it uses action=”send.php” to load the send.php page

 

The send.php page collects the form data and uses phpmailer to send the email, which it does successfully.  But it does not then display the thankyou.php page

 

Hopefully the gurus on this forum will spot the error I have made super-fast, such is my confidence in your talents!  My contact form is at

 

http://dave.higgsy.com/form/contact_form.php

 

Many thanks in advance

david

    This topic has been closed for replies.
    Correct answer osgood_

    Your thank you page wont work because you are echoing content to the page before the header location function in the phpmailer script. I would also suggest to make sure there is no white gaps between the php script lines when using the php header location function.

     

    You dont need the echo line in the php script. As you are using the php header location function you can inform the sender of the form information that the process has been successful on the thank you page

     

    And yes if you are including the php script in a page where html exists and youre using the php header location function then its best to include the script at the very top of your page to avoid potential issues that 'headers have already been sent' problem.

    2 replies

    higgsyd
    higgsydAuthor
    Inspiring
    May 8, 2021

    Thanks nancy, I forgot you couldn't see the PHP.  Hereis the form:

     

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<script src="../js/jquery.js"></script>
    	<script>
    		// here is showver and hidever	
    		$(document).ready(function() {
    		function showver() {
    			$('#trigger').show();
    		}
    		function hidever() {
    			$('#trigger').hide();
    		}
    		$('#trigger').hide();
    		$('#trigger').hover(showver,hidever);
    		});
    	</script>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<title>higgsy - Contact</title>
    	<link href="../css/xampp_higgsy_new.css" rel="stylesheet" type="text/css" />
    	<link href="../css/higgs.css" rel="stylesheet" type="text/css" />
    	<style>
    		.push_form {
    	margin-left: 300px;
    	margin-top: 50px;
    			}
    		.detail {
    			width:260px;
    			padding: 7px 8px;
    			margin: 0;
    			display: block;
    			}
    	</style>
    
    </head>
    
    <body>
    <?php include_once("../analyticstracking_DAV.php") ?>
    <div class="container">
    
      <div class="sidebar1">
        <ul class="nav">
          <li><a href="../index.php">Home</a></li>
    
          <li><a href="../adoption/10_adoption.php">Adoption</a></li>
          <li><a href="../ancestry/10_ancestry.php">Ancestry</a></li>
          <li><a href="../Drinking/10_drinks_index.php">Drinking</a></li>
          <li><a href="../dvds/05_login_url.php">DVDs</a></li>
          <li><a href="../garage/garage.php">Garage</a></li>
          <li><a href="../garden/garden.php">Garden</a></li>
          <li><a href="../france/france.php">France</a></li>
          <li><a href="../work/100_work_bookbinding.php">Work</a></li>
          <li><a href="../schooldays/10_school.php">School</a></li>
          <li><a href="../journey/journey.php">Journey</a></li>
          <li><a href="../2018.php">2018</a></li>
          <li><a href="contact_form.php">Contact</a></li>
        </ul>
       </div> <!-- end .sidebar1 -->
    		
    	<div class="content">
    	
    	<!-- my Back/Next nav bar -->
    	<table class="navtable">
    		<tr>
    			<td>&nbsp;</td>
    			<td><a href="../index.php">Back</a></td>
    			<td>Next</td>
    			</tr>
    	</table>
    
    	<h2 class="centre_this_text">Contact</h2>
    	<div class="push_form">
    		<form method="post" action="send.php">
    			<input class="detail" type="text" name="name" placeholder="Your Name" required><br><br>
    
    			<input type="email" name="email" placeholder="Your Email" required><br><br>
    			<textarea name="message" placeholder="Your Message" required></textarea><br><br>
    <!--			<input type="hidden" name="phone2">-->
    			<button type="submit">Send Email</button>
    		</form>
    	</div>
    	
    	</div><!-- end .content -->
    		
    	<div class="footer">
    		<?php include("../includes/footer.htm"); ?>
    	</div> <!-- end .footer -->
        
    </div> <!-- end .container -->
    
    </body>
    </html>

     

    and here is the send.php with the non-functioning PHP header statement at the bottom of the code. 

     

     

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<script src="../js/jquery.js"></script>
    	<script>
    // here is showver and hidever	
    $(document).ready(function() {
    	
    function showver() {
    	$('#trigger').show();
    }
    
    function hidever() {
    	$('#trigger').hide();
    }
    
    $('#trigger').hide();
    
    $('#trigger').hover(showver,hidever);
    	
    });
    </script>
    	
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<title>higgsy - Contact</title>
    	
    	
    	<link href="../css/xampp_higgsy_new.css" rel="stylesheet" type="text/css" />
    	<link href="../css/higgs.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
     <?php include_once("../analyticstracking_DAV.php") ?>
    <div class="container">
    
      <div class="sidebar1">
        <ul class="nav">
          <li><a href="../index.php">Home</a></li>
    
          <li><a href="../adoption/10_adoption.php">Adoption</a></li>
          <li><a href="../ancestry/10_ancestry.php">Ancestry</a></li>
          <li><a href="../Drinking/10_drinks_index.php">Drinking</a></li>
          <li><a href="../dvds/05_login_url.php">DVDs</a></li>
          <li><a href="../garage/garage.php">Garage</a></li>
          <li><a href="../garden/garden.php">Garden</a></li>
          <li><a href="../france/france.php">France</a></li>
          <li><a href="../work/100_work_bookbinding.php">Work</a></li>
          <li><a href="../schooldays/10_school.php">School</a></li>
          <li><a href="../journey/journey.php">Journey</a></li>
          <li><a href="../2018.php">2018</a></li>
          <li><a href="contact_form.php">Contact</a></li>
        </ul>
       </div> <!-- end .sidebar1 -->
    	 		
    		
    		
    	<div class="content">
    	
    	<!-- my Back/Next nav bar -->
    	<table class="navtable">
    		<tr>
    			<td>&nbsp;</td>
    			<td><a href="../index.php">Back</a></td>
    			<td>Next</td>
    			</tr>
    	</table>
    			
    <?php
    	
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require '../vendor/autoload.php';
    	
    	$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    	$phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
    	$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
    	$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
    	
    	$mail = new PHPMailer(true);
    
    try {
    	error_log( "DH0010 - send.php entered v7", 0 ); // works!!
    	
        $mail->setFrom('email@example.com [real email removed by moderator]');
        $mail->addAddress($email, $name);     //Add a recipient
    	//Body content
    	$body = "<p><strong>Hello</strong>, enquiry from " . $name . " the msg is " . $message . "</p>";
    	//Content
    	$mail->isHTML(true);
    	$mail->Subject = 'Website Enquiry from ' . $name;
    	$mail->Body    = $body;
    	$mail->AltBody = strip_tags($body);
        $mail->send();
    	echo "email sent";
    	//header('location: http://example.com [real URL removed by moderator]');
    	header('location: thankyou.php');
    	error_log( "DH0020 - send.php finished...", 0 ); // works!!
    	
    } catch (Exception $e) {
        echo "Message could not be sent. MMailer Error: {$mail->ErrorInfo}";
    }  // close the catch
    	
    ?> 
    	</div><!-- end .content -->
    		
    	<div class="footer">
        <?php include("../includes/footer.htm"); ?>
      </div> <!-- end .footer -->
    	  
    </div> <!-- end .container -->
    
    </body>
    </html>

     

    I am wondering if my PHP is in the correct place as I seem to recall there is a case where you have to put the PHP right at the top of the document, before the DOCTYPE - is my code ok where i've put it?

    Also your example code looks very useful and I'll take closer look once I'm clear of this present problem!

    many thanks

    David

    osgood_Correct answer
    Legend
    May 9, 2021

    Your thank you page wont work because you are echoing content to the page before the header location function in the phpmailer script. I would also suggest to make sure there is no white gaps between the php script lines when using the php header location function.

     

    You dont need the echo line in the php script. As you are using the php header location function you can inform the sender of the form information that the process has been successful on the thank you page

     

    And yes if you are including the php script in a page where html exists and youre using the php header location function then its best to include the script at the very top of your page to avoid potential issues that 'headers have already been sent' problem.

    higgsyd
    higgsydAuthor
    Inspiring
    May 12, 2021

    well done Osgood, once again you have come up trumps, it all works fine now - and thanks to Nancy to for her help, what great stars you all are!

    Nancy OShea
    Community Expert
    Community Expert
    May 8, 2021

    We cannot see your PHP code online.  You need to post the entire code here for us to see.

     

    If you provide feedback on the form itself, a separate success page is unnessary.

    This is an old tutorial but it demonstrates how to have feedback appear on the contact form using PHP.

    https://alt-web.blogspot.com/2015/07/responsive-contact-form-with-bootstrap.html

     

     

     

     

    Nancy O'Shea— Product User & Community Expert