0

I have a question how can I redirect in PHP after a form insert.

Example: I have the file insertMedia.php

The HTML:

<form method="POST" action="insertMedia.php">

</form>

and after the html the php script:

<?php
code_for_mysql_insert(); // dummy placeholder function for the MySQL insert
header('Location: otherSite.php');
?>

But this doesn't work. What do I have to change?

Thanks for your help Lingo

Fabian Fagerholm
  • 4,099
  • 1
  • 35
  • 45
Lingo
  • 580
  • 2
  • 7
  • 26
  • What is `'insert`? – Thamilhan May 25 '16 at 10:05
  • 'insert is a placeholder for my MySQL insert. – Lingo May 25 '16 at 10:08
  • You can do it by Javascript, BUT why? You display a form and change the page short after this. Nobody will ever see the form ... – Marcus May 25 '16 at 10:09
  • I'm guessing you're attempting to do a header redirect *after* output has begun : http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php - just post the form to a separate processing script that has no output and do the redirect there (prevents caching on the client as well and avoids multiple submissions by reload) – CD001 May 25 '16 at 10:10
  • @Glufu the form is only to fill my MySQL insert query where my placeholder 'insert is. after that I wanna respond to an other site. Can you post me a link with an example to do that with javascript? – Lingo May 25 '16 at 10:11
  • you need the header('Location: otherSite.php'); to be in the php page that processes your form inputs - so the workflow - is html page for the form - clientside validation to ensure orrect form inputs - pass the form values to the php page - server side validation for correct values , alteration of db from the form inputs - redirect to page following db update – gavgrif May 25 '16 at 10:12

4 Answers4

2

You can try using JavaScript

<?php echo '<script>window.location = "http://www.google.com/" </script>';?>

If your form action link is like such

<form action="localhost/index.php?redirect=index2" method="post">

You can use this solution:

<?php  $link = "http://localhost/".$_GET['redirect'].".php";
echo '<script>window.location ="'.$link.'"</script>';?>
ranul
  • 123
  • 2
  • 10
bradley546994
  • 630
  • 2
  • 12
  • 30
0

There is no problem with the script. As long as your MySQL insert placeholder doesn't echo anything out.

Soon as headers are sent , the PHP header function stops working.

<?php
count([]);
header('Location: local.com.php');

Above is a script I tested. Count function doesn't echo anything out. I used it in place of the MySQL insert.

Gayan Hewa
  • 2,277
  • 4
  • 22
  • 41
0

Javascript Solution is

<script type="text/javascript">
  window.location.href = "otherSite.php";
</script>

But yout form isn't submited this way. You can add a GET to the URL.

Or you can use Javascript to send your form by calling the submit function of your form.

Marcus
  • 1,910
  • 2
  • 16
  • 27
-1

just use header('Location: otherSite.php'); remove insert

Maasoud Asadi
  • 61
  • 4
  • 8