Help with header(location)

Hi Guys,

After many hours research I still can’t get my head around redirecting with header(location) and so am hoping someone out there would be kind enough to help me out?

Basically i am posting form results to a url and waiting for a response. If the response is yes then i would like to redirect the user to a url that was sent back with the response, otherwise the results are posted to a 2nd url and if the response from that is yes i would like to redirect the user to a url that was sent back with that response etc… etc…


<body>
<?php
if(isset($_POST['submit'])){
	
$email = $_POST['email'];
$title = $_POST['title'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

$ip = $_SERVER['REMOTE_ADDR'];

$id = $_SESSION['id'];
$date = date("Y-m-d");
$time = date("H:i:s");
	
//DO CURL STUFF

	if ($response == "true"){
                $xml = new SimpleXMLElement($leadResult);
                $url = $xml->Redirect;
		header("Location: $url"); // if i get a positive response i want to redirect
	}	
	else {	

//DO MORE CURL STUFF

	if ($response == "true") {
                $xml = new SimpleXMLElement($leadResult);
                $url = $xml->Redirect;
		header("Location: $url"); // if i get a positive response i want to redirect
	}
	else {
		($response == "false");
	}
	}
// WRITE RESULTS TO DATABASE
}
?>
</body>

I realise that this won’t work, i’m just not sure why it won’t work and, more importantly, how to make it work.

Thanks in advance.

Try putting the php code at the top of the script (before any output has been generated)

Thanks for the reply. So you mean:


<?php
if(isset($_POST['submit'])){
	
$email = $_POST['email'];
$title = $_POST['title'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

$ip = $_SERVER['REMOTE_ADDR'];

$id = $_SESSION['id'];
$date = date("Y-m-d");
$time = date("H:i:s");
	
//DO CURL STUFF

	if ($response == "true"){
                $xml = new SimpleXMLElement($leadResult);
                $url = $xml->Redirect;
		header("Location: $url"); // if i get a positive response i want to redirect
	}	
	else {	

//DO MORE CURL STUFF

	if ($response == "true") {
                $xml = new SimpleXMLElement($leadResult);
                $url = $xml->Redirect;
		header("Location: $url"); // if i get a positive response i want to redirect
	}
	else {
		($response == "false");
	}
	}
// WRITE RESULTS TO DATABASE
}
?>
<html>
<body>
</body>
</html>

will my results still be written to the database if i do this?

Try it :slight_smile:

Maybe you’d have to save the results to the database before doing the redirect.

trial and error is my life! but thanks - i tried and it works :slight_smile: