How to redirect to webpage after html form submit with php?

373 views Asked by At

So I have a form with action insert.php

My insert.php looks like this

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

mysql_select_db("watchdl1_database", $con);

$sql="INSERT INTO nametable (firstname, lastname)
VALUES
('$_POST[firstname]','$_POST[lastname]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>
</body>
</html>

How can I get the user to be redirected to a url after the html form submits to the database using this php code?

Thanks.

EDIT: I get this error Parse error: syntax error, unexpected 'header' (T_STRING) in insert.php

when ever I try to add header command.

2

There are 2 answers

3
Natus On
  1. php

    header('Location: example.php');

  2. javascript

    window.location="example.php";

1
Mahmoud Kamel On

in php : header("Location: redirect_page");

in javascript: window.location.href ="redirect_page";