Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php return to page after form submission

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?

like image 578
Carla Dessi Avatar asked Apr 12 '14 19:04

Carla Dessi


People also ask

How to redirect to url after form submission in PHP?

PHP: Redirect To URL After Form Submission. Now in PHP, redirection is done by using header() function as it is considered to be the fastest method to redirect traffic from one web page to another. The main advantage of this method is that it can navigate from one location to another without the user having to click on a link or button.

How to return to the previous page in PHP?

This article will introduce some methods to return to the previous page in PHP. The HTTP_REFERER request header returns the URL of the page from where the current page was requested in PHP. The header enables the server to acknowledge the location from where the users are visiting the current page.

How to trap the submit button press in PHP?

Instead of using the normal form submission via HTML, you would have some JavaScript code that traps the submit button press. That code would grab the data from the form, call the PHP code, await the response from the PHP, and then act accordingly.

How do I redirect to a specific page in PHP?

Redirection is done by outputting a Location using PHP using the header () function. Here's how to redirect to a page called thanks.html: Don't output any content to the browser via echo () or print (), or by including HTML markup outside the <?php ... ?> tags before calling header ().


2 Answers

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.

like image 125
Jared Farrish Avatar answered Oct 05 '22 18:10

Jared Farrish


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.

like image 24
Ashley Avatar answered Oct 05 '22 17:10

Ashley