Php header redirect not working


#1

Hi,
I’m new to web design and php, so, sorry if there is something easy I’ve overlooked here. I’m trying to design a simple email contact form - the contact form itself actually DOES work. When I submit the form I do receive emails (with the code below with correct address), but the problem is the page then does not redirect to a ‘contact submitted’ page on my website. Nothing happens and it stays on the same contact form page (although it looks like it is trying to connect for a second). I have spent a long time looking online and trying different things, but nothing works and I am wondering whether it is something to do with limitations of a free 000webhost account (do I have to edit htaccess?). I have tried both header() and echo<> (see code) and I don’t think I have echoed anything before the header(). I know that I still have to fix the POST for sql injection. I have other forms on my website that post data to my sql database and then redirect without any problems. I don’t really want to upgrade my account yet as this website is a test run. I’d appreciate any advice.Thanks!


<head>
<title>Contact us</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

</head>
<body >
		<form action="formSubmit.php" method="post">
	
	<div>
		<label class="description" for="first_name">First name</label>
		<p><input id="first_name" type="text" name= "first_name" class="element text large" maxlength="255" value="" required /></p>
	</div>
	<div>
		<label class="description" for="last_name">Last name</label>
		<p><input id="last_name" type="text" name= "last_name" class="element text large" maxlength="255" value="" required /></p>
	</div>
	<div> 
		<label class="description" for="email">email</label>
		<p><input id="email" type="text" name= "email" class="element text large" maxlength="255" value="" required /></p>
		</div>
		<div> 
		<label class="description" for="comments">comments</label>
		<p><input id="comments" type="text" name= "comments" class="element text large" maxlength="255" value="" required /></p>
		</div>	
		
			<div>
			<br>
				<li class="buttons">
				<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
				<input id="reset"  class="button_text" type="reset" value="Start Over">
				</div>
		</li>	</form>
								</body>

ob_start();
error_reporting(E_ALL); 
/* Set e-mail recipient */
$myemail = "myemail@address.com"; 

 $first_name = $_POST['first_name']; // required
 $last_name = $_POST['last_name']; // required
 $email_from = $_POST['email']; // required
 $comments = $_POST['comments']; // required
 $subject= $_POST['comments']; // required
	 
/* prepare the message for the e-mail */
$message = "

Name: $first_name
E-mail: $email_from
Subject: $comments

Message:
$comments
";

/* Send the message using mail() function */
$headers = 'From: '.$email_from."
";
mail($myemail, $subject, $message,$headers);

/* Redirect visitor to the thank you page */
header('Location:  http://.../contact-submitted.html');
//echo "<meta http-equiv='Refresh' content='0; URL=http://.../contact-submitted.html'>"; 
ob_end_flush(); 
exit();

#2

I have also disabled the analytics.


#3

Just tested your contact form and was redirected to http://****.freeiz.com/contact-submitted.html right after submitting it.


#4

Fantastic - thanks so much for testing it and great to know it seems to work. I don’t know why that isn’t happening when I try from my end - frustrating not to see it working myself. I’ll give it another go.


#5

Try clearing your browser cache, and it may magically start working :wink:


#6

Thanks for the tip! I cleared everything, several times, but I’m still not being redirected on my end. I’ve tried in firefox, IE and chrome. Going to take a break and work on fixing some other things for now - will be getting some colleagues to test the site soon so I"ll see if they have any luck.