0

i am trying to redirect after submitting some data into my database to example.php i have the form and it submits perfect but cant get the redirect to work i have tried a few methods i have seen on the internet but nothing worked

<html>
    <head>

        <title>MySQLi Create Record</title>

    </head>
<body>

<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";

if($action=='create'){
        //include database connection
        include 'db_connect.php';

        //write query
        $query = "insert into referb 
                                set
                                        make = '".$mysqli->real_escape_string($_POST['make'])."', 
                                        model = '".$mysqli->real_escape_string($_POST['model'])."',
                                        unitt  = '".$mysqli->real_escape_string($_POST['unitt'])."'";

        if( $mysqli->query($query) ) {
                echo " header( 'Location: example.php' ) ;";
        }else{
                echo "Database Error: Unable to create record.";
        }
        $mysqli->close();
}

?>

<!--we have our html form here where user information will be entered-->
<form action='#' method='post' border='0'>
    <table>
        <tr>
            <td>Manufactor</td>
            <td><input type='text' name='make' /></td>
        </tr>
        <tr>
            <td>Model</td>
            <td><input type='text' name='model' /></td>
        </tr>
        <tr>
            <td>Unit Type</td>
            <td>
            <select name='unitt'>
  <option>Laptop</option>
  <option>Computer</option>
  <option>Tablet</option>
</select></td>
        </tr>

            <td></td>
            <td>
                <input type='hidden' name='action' value='create' />
                <input type='submit' value='Save' />

                                <a href='index.php'>Back to index</a>
            </td>
        </tr>
    </table>
</form>

</body>
</html>
  • header() is a php function and not an html tag. you simply have to call it like header( 'Location: example.php' ) ; – pinkal vansia Aug 17 '13 at 15:03
  • First, even if you DID get this to work, you have to remove the space between your `header(` and `'Location:` as you have here `echo " header( 'Location: example.php' ) ;";` - it needs to read as `header('Location:` - however the correct way of doing a header redirect is `header('Location: example.php');` – Funk Forty Niner Aug 17 '13 at 16:09

5 Answers5

1

the call to header() should not be echo'd. that line should be changed to:

header('Location: example.php');
exit();

calling exit() afterards will then stop further execution of the script.

Nathan
  • 1,700
  • 12
  • 15
  • 1
    To avoid the next pitfall, this must be placed **before** any HTML output. That means the code with the redirect first, the `` tag afterwards. – martinstoeckli Aug 17 '13 at 17:16
0

Do not echo header("Location: example.php)". It is a php function so you just write

header("Location:example.php");
Sasanka Panguluri
  • 3,058
  • 4
  • 32
  • 54
0

You cannot have echo before header or together inside the same directive/condition.

You can however setup an if directive.

You have echo " header( 'Location: example.php' ) ;";

it would need to read as header('Location: file.php'); only.

The correct method of doing a header redirect is:

header('Location: example.php');

You can do the following:

if( $mysqli->query($query) ) {
    header('Location: example.php');
    }else{
            echo "Database Error: Unable to create record.";
        }
    $mysqli->close();
}

Visit the PHP.net on the header() function: http://php.net/manual/en/function.header.php

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I just read through the docs and don't see where `header( 'Location: example.php' );` with the spaces is invalid or that it will fail. Tested it locally and it works with / without spaces. Do you have a reference for this, just curious? – Sean Aug 17 '13 at 16:55
  • @Sean Yes, if you visit this link http://php.net/manual/en/function.header.php and I quote: *"you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent."* Notice the space between `header(` and `header(` and 'Location: ...' )` – Funk Forty Niner Aug 17 '13 at 19:09
  • @Sean Actually, you can't have `echo` and `header` together, because `echo` is considered as "output before headers". – Funk Forty Niner Aug 17 '13 at 19:17
0

Step 1: First execute your query to insert data into database

Step 2: Close database connection

Step 3: Just put>>> header('Location: example.php');

Krypton
  • 3,337
  • 5
  • 32
  • 52
Narad
  • 1
  • 1
-4

this a php script that redirect the user to another URL put this after the right condition or after instructions of your code

 <html>
 <?php
 header('Location: http://www.example.com/');
 exit;
 ?>
  • 2
    This will not work. [Read more why](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – Glavić Aug 17 '13 at 15:05
  • This is the first example in the docs - http://php.net/manual/en/function.header.php - and specifically says in the example - `This will give an error. Note the output above, which is before the header() call`. And before the example - `Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.` – Sean Aug 17 '13 at 16:46