-6

I'm making a basic site with PHP. Here, I have code that will not work. I want to redirect a page to another page.

<?php

$k = mysqli_connect("localhost","root","","ubm_2015");

if(!$k)
{
    echo "Koneksi Gagal <br>";
    echo mysqli_errno();
}
else
{   echo "Koneksi Berhasil";
}

$nama = $_POST['nama'];
$motor = $_POST['vhc'];
$hobi = $_POST['hobi'];

$query = "insert into data_mhs(nama,motor,hobi) values ('$nama','$motor','$hobi')";

mysqli_query($k,$query);

echo " Query Berhasil ";

header("Location : select.php");


?>

I use header, but it's not working.

Are there any tips to redirect in PHP?

Pang
  • 9,564
  • 146
  • 81
  • 122
LStackL
  • 33
  • 7
  • 3
    Possible duplicate of [Php header location redirect not working](http://stackoverflow.com/questions/21226166/php-header-location-redirect-not-working) – Suyog Nov 02 '15 at 09:03
  • You probably can't set headers because you spit out the body with echo(). BTW, please always give the actual results (e.g. in the browser) that you receiving. – pwes Nov 02 '15 at 09:04
  • remove all `echo` and `print` statement before echo.it means there should not be any output before header otherwise it will not redirect. – Suchit kumar Nov 02 '15 at 10:02
  • don't give spaces in header, try this: `header("Location:select.php");` – Jason Brody Nov 02 '15 at 10:41

5 Answers5

0

You can't use header after an echo since the page will already be created by those echo, it should even return you the following error :

Warning : Cannot modify header information – headers already sent by (your file)

You have to remove all your echo first, then it should work like a charm

You have to remove those lines with echos :

if(!$k)
{
   // echo "Koneksi Gagal <br>";
   // echo mysqli_errno();
}
else
{ 
 //echo "Koneksi Berhasil";
}


// echo " Query Berhasil ";
Nirnae
  • 1,315
  • 11
  • 23
0
if(!$k)
{
    echo "Koneksi Gagal <br>";
    echo mysqli_errno();
}
else
{   echo "Koneksi Berhasil";
}

Either of your if-else will run and write something to the response.

you can't redirect after writing the response.

To redirect set header("Location : select.php"); before echo or any other output

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
0

Try replacing the header with this line:

 echo "<META http-equiv='refresh' content='5;URL=select.php'>";
Joe Scotto
  • 10,936
  • 14
  • 66
  • 136
0

you have to call javascript function for that,just try with this:-

echo "<script>
function myFunction() 
{
setTimeout(function(){ location.href='your file name'; }, 5000);
}
myFunction();
    </script>";

and you are writing "select.php" but if you are working on localhost then you have to write like this:-

location.href='http://localhost/folder_name if you have/select.php'

and if you are working on server then it would be:-

location.href='http://host ip/folder name/select.php'
Sakura
  • 93
  • 1
  • 1
  • 10
0
<?php

$k = mysqli_connect("localhost","root","","ubm_2015");

if(!$k)
{
    echo "Koneksi Gagal <br>";
    echo mysqli_errno();
}
else
{   echo "Koneksi Berhasil";
}

$nama = $_POST['nama'];
$motor = $_POST['vhc'];
$hobi = $_POST['hobi'];

$query = "insert into data_mhs(nama,motor,hobi) values ('$nama','$motor','$hobi')";

mysqli_query($k,$query);

// echo " Query Berhasil ";

header("Location : select.php");


?>

It's because you used echo. The header function would not be run because the echo is run first. You can't execute an echo with a header at the same time, so the echo is run first and the header will be ignored. If you want to execute the echo in the new file, maybe you can use the _GET function.

In the actual file:

header("Location : select.php?echo=Query%20Berhasil");

In select.php:

$echo = $_GET['echo'];

Hope I could help you, else as the Meta Refresh, I would recommend to use the Header function instead.

Pang
  • 9,564
  • 146
  • 81
  • 122