9

I've created a html form

<form action="http://localhost/php/insert.php" method="post">
    Barcode: <input type="text" name="barcode" /><br><br>
    Serial: <input type="text" name="serial" /><br><br>
    <input type="submit" />
</form>

which saves into my database using this php:

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

$sql = "INSERT INTO asset (barcode, serial)
        VALUES ('$_POST[barcode]','$_POST[serial]')";

if ( ! mysql_query($sql, $con)) {
    die('Error: ' . mysql_error());
}
mysql_close($con)
?>

the form saves to the DB but the page just goes to http://localhost/php/insert.php when you press submit, which is a blank page. how can I make it stay on the same page and just show a message or something?

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
Carla Dessi
  • 9,086
  • 9
  • 39
  • 53

4 Answers4

8

Note that redirecting with HTTP_REFERER is flaky and untrustworthy in general, so the best way is to have a URL sent with the request or, if it's coming from another server, you could build a specific pass-through to use for specific sites/URLs. The best is to know the URL by having it be submitted with the request; the other two are probably less than optimal.

After your form processing is done, do this before outputting anything (which will throw an error):

header("Location: {$_SERVER['HTTP_REFERER']}");
exit;

Depending on what your processing outcomes could be, you could append an error to the URL:

if ($query_does_not_execute) {
    $errcode = "error_code=003";
}

$referer = $_SERVER['HTTP_REFERER'];

if ($errcode) {
    if (strpos($referer, '?') === false) {
        $referer .= "?";
    }

    header("Location: $referer&$errcode");
} else {
    header("Location: $referer");
}
exit;

Then on the page, you could show the error. Or, alternatively, you could redirect to a page to handler errors of different kinds. This is wide-open to a number of different interpretations.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • yeh I get `Undefined index: REFERER` – Carla Dessi Apr 12 '14 at 19:32
  • If it's coming from a HTTPS page REFERER will not be available, thus it's impossible to retrieve it from $_SERVER. – Ashley Apr 12 '14 at 19:35
  • @CarlaDessi - I had the wrong key, it's `HTTP_REFERER`. Note that it's also somewhat flaky, since it can be set by the browser (or not set, or spoofed). – Jared Farrish Apr 12 '14 at 19:36
  • that should do the job, thanks! is there a way I can show a confirmation dialogue after? – Carla Dessi Apr 12 '14 at 19:44
  • @Ashley - Where do you come up with this `HTTPS` blocks `HTTP_REFERER`? Do you have any documentation stating this outright? – Jared Farrish Apr 12 '14 at 19:44
  • @CarlaDessi - This is very rudimentary, just to give you the idea. If it's redirecting back to another site you don't have control over, you're going to need to show something on your site's page, then [redirect within the page](http://stackoverflow.com/a/17150241/451969) after so many seconds. You would not use `header()` for this, though. – Jared Farrish Apr 12 '14 at 19:48
  • @CarlaDessi - This would be a basic example of a redirect with message: http://jsfiddle.net/FP3jw/ – Jared Farrish Apr 12 '14 at 19:53
4

You can use PHP's Header() function like so:

header("Location: your-page.php");
exit;

You'll need to make sure there is no output (i.e. echoing anything, or any HTML etc) before the header call or it will not work.

Ashley
  • 1,459
  • 2
  • 12
  • 25
0

Another way I found works is redirecting with html's <meta>. Put it in your http://localhost/php/insert.php script . After processing redirect to desired page.

<?php 

switch ($INPUT['task'])
{
    case 'insert_and_redirect':
            insertForm();
            $h = '<html><head>';
            $h .=   '<meta http-equiv="refresh" content="0; URL=/adjustments/">';
            $h .=   '</head></html>';
            echo $h;
        break;
    case 'edit':
       # editForm();
        break;
    case 'delete':
        deleteRecord();
        break;
    default:
        break;
}

?>

Probably unsecure but if you're on a private LAN should be fine.
-Cheers

MattJamison
  • 311
  • 3
  • 11
-1

Use this in your form action $_SERVER['PHP_SELF']; it will fulfill your requirement.

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
Parimal
  • 183
  • 1
  • 11