Email Form - Redirect after Submit

Good day all,

Well, my problem is:
When I click submit on my contact form, I want it to send the email (which i’ve managed to do) but then at the end of the php I want it to go back to the page it came from. Or the REFERER.

So I have this code:
[php]//if this is not an ajax request
if(empty($_SERVER[‘HTTP_X_REQUESTED_WITH’]) && strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) !== ‘xmlhttprequest’){
//set session variables
session_start();
$_SESSION[‘cf_returndata’] = $returndata;

	//redirect back to form
	header('location: ' . $_SERVER['HTTP_REFERER']);
}[/php]

But I get this response when submitting the form. Which, I understand, it isn’t able to execute due to the location already being set at a point before this request.

Notice: Undefined index: HTTP_X_REQUESTED_WITH in /customers/3/8/a/care2pets.co.uk/httpd.www/enquiry.php on line 29 Warning: Cannot modify header information - headers already sent by (output started at /customers/3/8/a/care2pets.co.uk/httpd.www/enquiry.php:29) in /customers/3/8/a/care2pets.co.uk/httpd.www/enquiry.php on line 31
Here's the full code snippet. [php] <?php
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');

error_reporting(E_ALL); 
ini_set('error_reporting', E_ALL);
ini_set('display_errors',1);

$name = $_POST['name'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$location = $_POST['location'];
$enquiry = $_POST['enquiry'];

$from = 'From: '.$email; 
$to = '[email protected]'; 
$subject = 'Care2Pets Website Enquiry';
$message = "From: $name\n E-Mail: $email\n Telephone: $telephone\n Location: $location\n Message:\n $enquiry\n This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}";
$headers = 'From: [email protected]' . "\r\n" . 
'Reply-To: [email protected]' . "\r\n" . 
'X-Mailer: PHP/' . phpversion(); 

if (!empty($_POST['name'])) {				 
    mail ($to, $subject, $message, $from);
}	
	//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
	//set session variables
	session_start();
	$_SESSION['cf_returndata'] = $returndata;
	
	//redirect back to form
	header('location: ' . $_SERVER['HTTP_REFERER']);
}

?>
[/php]

Any help or other solutions would be greatly appreciated…

Does your PHP script look exactly like what you posted? if so the first line is your problem. Having anything (including spaces) outside of php execution is considered output for the browser, so in this case you can not send header info because content has already been sent.

This is also the reason you should not end php execution (?>) when it’s not necessary

[php]$name = $_POST[‘name’];
$telephone = $_POST[‘telephone’];
$email = $_POST[‘email’];
$location = $_POST[‘location’];
$enquiry = $_POST[‘enquiry’];[/php]This should be changed to add a fail safe if the fields are not submitted

[php]$name = !mpty($_POST[‘name’]) ? $_POST[‘name’] : ‘Not submitted’;
etc…[/php]

[php]if(empty($_SERVER[‘HTTP_X_REQUESTED_WITH’]) && strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) !== ‘xmlhttprequest’){[/php]Here you say if this server variable is empty, use the server variable. I think you need an “not” (!) before that empty.

Thanks for getting back to me JimL.

I believe I don’t have spaces at the start of my php. I think that’s was a C&P error.

So I shall remove the ?> at the end of my file?
And add a ! before empty for a “not empty”

I’ll have a go at this when I get in from work.

Ok, then move the set header line upwards in the script and figure out the last point where you are able to send headers without getting an error. Just need to research/debug to figure out where the error is.

And yes, just leave php execution on, it is considered good practice

I think you should change it to not empty because it doesn’t make sense to have a condition checking if its empty AND that its lower case value is (not) equal to something

Ok, so I did what you said (aside from the validation) and the form submitted, but remained on the enquiry.php page. Presumably, because it hit the if(!empty statement and didn’t execute it. Suggesting that value is set. So when I remove the ‘!’ from the statement it goes back to square one with the below error.

Notice: Undefined index: HTTP_X_REQUESTED_WITH in /customers/3/8/a/care2pets.co.uk/httpd.www/enquiry.php on line 23 Warning: Cannot modify header information - headers already sent by (output started at /customers/3/8/a/care2pets.co.uk/httpd.www/enquiry.php:23) in /customers/3/8/a/care2pets.co.uk/httpd.www/enquiry.php on line 24
I've included my current code below too.

[php]<?php
$ipaddress = $_SERVER[‘REMOTE_ADDR’];
$date = date(‘d/m/Y’);
$time = date(‘H:i:s’);
error_reporting(E_ALL);
ini_set(‘error_reporting’, E_ALL);
ini_set(‘display_errors’,1);
$name = $_POST[‘name’];
$telephone = $_POST[‘telephone’];
$email = $_POST[‘email’];
$location = $_POST[‘location’];
$enquiry = $_POST[‘enquiry’];
$from = 'From: '.$email;
$to = ‘[email protected]’;
$subject = ‘Care2Pets Website Enquiry’;
$message = “From: $name\n E-Mail: $email\n Telephone: $telephone\n Location: $location\n Message:\n $enquiry\n This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}”;
$headers = ‘From: [email protected]’ . “\r\n” .
‘Reply-To: [email protected]’ . “\r\n” .
‘X-Mailer: PHP/’ . phpversion();
if (!empty($_POST[‘name’])) {
mail ($to, $subject, $message, $from);
}
if(empty($_SERVER[‘HTTP_X_REQUESTED_WITH’]) && strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) !== ‘xmlhttprequest’){
header('location: ’ . $_SERVER[‘HTTP_REFERER’]);
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service