0

I have a problem which i can't resolve. I want to redirect to an URL after i do a form post (button clicked). How can i do that? I didn't find a solution so far on the internet.

My files:

addmatch.html

<html>
<body>
<form action="insert.php" method="post">
Date: <input type="text" name="date">
Home: <input type="text" name="home">
Away: <input type="text" name="away">
Result: <input type="text" name="result">
<input type="submit" name="submit">
</form>

</body>
</html>

insert.php

<?php

define("HOST", "localhost");
define("DBUSER", "..");
define("PASS", "..");
define("DB", "..");

$con=mysqli_connect(HOST, DBUSER, PASS, DB);

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO wedstrijden (date, home, away, result)
VALUES
('$_POST[date]','$_POST[home]','$_POST[away]','$_POST[result]')";

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

mysqli_close($con);
?>

Thanks in advance!

Teddy
  • 23
  • 1
  • 4
  • use header('Location: ur_path'); –  Oct 04 '13 at 09:01
  • People use javascript to do that. Use somethin like `window.location.assign("your-url-goes-here")` – venkatKA Oct 04 '13 at 09:02
  • @zander It's down to the individual situation, sometimes PHP might be more appropriate. I.E. Javascript can be disabled. – George Oct 04 '13 at 09:04
  • Ooh then header(..) should do – venkatKA Oct 04 '13 at 09:06
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) against. – Quentin Oct 04 '13 at 09:09
  • possible duplicate of [How to make a redirect in PHP?](http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) – Quentin Oct 04 '13 at 09:10

1 Answers1

1

as I can see now, you form will send post request to insert.php if you want to redirect user after you will save data in database you should add header("location youUrl.php"); before any output (echo, print etc)

Liauchuk Ivan
  • 1,913
  • 2
  • 13
  • 22