nogggin1
nogggin1

Reputation: 13

Redirect PHP after script is run

I have a script that submits emails entered into a form to a database and I am wondering if there is a way to redirect the PHP script after the email has been submitted?

my script is -

<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("emails", $con);

$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con)
?> 

please could someone give clear instructions as I am learning PHP still.

Upvotes: 1

Views: 13748

Answers (7)

Gareth Drew
Gareth Drew

Reputation: 106

Redirect in the controller.

header('Location: http://www.yoursite.com/new_page.html');

Upvotes: 0

khoa vo
khoa vo

Reputation: 1

Add send header function

<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("emails", $con);

$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }else{
header("Location: ".$_SERVER["HTTP_REFERER"]);
}

mysql_close($con)
?> 

Or send javascripts with code

<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("emails", $con);

$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }else{

echo "<script>";
    echo "window.location.href= '".$_SERVER["HTTP_REFERER"]."';";
echo "</script>";

}

mysql_close($con)
?> 

Upvotes: 0

regilero
regilero

Reputation: 30496

To take this redirect one step further. The header "location" should be used with a 302 response code instead of a classical 200 response code. Thios response code is 'global' to the HTTP response. 302 means 'redirect'. Most browser handle the location header even without the 302 response code, but you should set it (it could help for example ajax handling of your response).

header("Location: /foo.php",TRUE,302);

Now one step more. In fact a 303 response code (see-other) is a better redirect response code. It's called as well the redirect after post. This special redirect means for your browser that it's not really a redirect because the page he asked whas in the wrong place, but that it's really a redirect because after your POST action you need a new page result.

header("Location: /foo.php",TRUE,303);

Edit: as stated by thirtydot, a better usage is absolute url in the redirect

header("Location: http://www.example.com/foo.php",TRUE,303);

Remember that POST implies potential data changes (that GET request should'nt imply). A POST followed by a 303 is the right way to go, and will prevent your BACK button reposting the same request.

Upvotes: 3

thirtydot
thirtydot

Reputation: 228162

This code taken from the header() manual page will redirect to another page on the same domain and in the same directory:

/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;

Upvotes: 1

Spiny Norman
Spiny Norman

Reputation: 8327

Use:

$url = 'http://your.redirect.url';
header('Location: ' . $url);

Upvotes: 2

Jake N
Jake N

Reputation: 10583

header("Location http://www.example.com");

That will redirect the browser to example.com, note that you can only use this if output has not already been sent to the browser, e.g make sure you have echo, print or var_dump statements before you use header()

Upvotes: 0

Diablo
Diablo

Reputation: 3418

Try this:

mysql_select_db("emails", $con);

$sql="INSERT INTO mlist (email) VALUES ('$_POST[email]'')";

if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }

mysql_close($con);

header('Location: http://www.example.com/');

Upvotes: 1

Related Questions