php return to page after form submission

Carla Dessi

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?

Jared Farrish

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

php return to page after form submission

From Dev

Can't get rid of blank php page after submission form

From Dev

Form submission opens php page

From Dev

Display the same page even after form submission

From Dev

redirecting to another page after form submission

From Dev

posting scroll position to return user to same place on page after form submission

From Dev

Form submission using ajax and page view moderation after the submission

From Dev

Form submission return me to index.php

From Dev

How to avoid page reload in php form submission

From Dev

Run PHP Code After Form Submission And Redirect

From Dev

Keeping values selected after form submission with php

From Dev

Run script and php after form submission

From Dev

How to show the image uploaded using HTML file control on a new page after form submission in PHP?

From Dev

php reCaptcha will not return confirmation after submission

From Dev

PHP login using session return blank page after submit form

From Dev

Clear the form field after successful submission of php form

From Dev

No response after form submission and page just auto reload

From Dev

Don't reload page after form submission using ajax and jquery

From Dev

How to redirect to new html page after form submission in Apps script

From Dev

Trigger a redirect to a page on form submission after three incorrect attempts in Javascript

From Dev

header() redirects page automatically instead of after submission of form

From Dev

How to show the success message of PHP in html page on Form submission

From Dev

Using jQuery AJAX to call a php page on form submission

From Dev

Why do I see a blank screen after form submission in PHP?

From Dev

PHP after form submission disappears instead of redirecting via header location

From Dev

How to show a success message after PHP form submission?

From Dev

Keep form data inside the field after submission using php

From Dev

how to clear form using php PDO after successful submission

From Dev

jquery ajax requests are not responding on the new tab after the php form submission

Related Related

  1. 1

    php return to page after form submission

  2. 2

    Can't get rid of blank php page after submission form

  3. 3

    Form submission opens php page

  4. 4

    Display the same page even after form submission

  5. 5

    redirecting to another page after form submission

  6. 6

    posting scroll position to return user to same place on page after form submission

  7. 7

    Form submission using ajax and page view moderation after the submission

  8. 8

    Form submission return me to index.php

  9. 9

    How to avoid page reload in php form submission

  10. 10

    Run PHP Code After Form Submission And Redirect

  11. 11

    Keeping values selected after form submission with php

  12. 12

    Run script and php after form submission

  13. 13

    How to show the image uploaded using HTML file control on a new page after form submission in PHP?

  14. 14

    php reCaptcha will not return confirmation after submission

  15. 15

    PHP login using session return blank page after submit form

  16. 16

    Clear the form field after successful submission of php form

  17. 17

    No response after form submission and page just auto reload

  18. 18

    Don't reload page after form submission using ajax and jquery

  19. 19

    How to redirect to new html page after form submission in Apps script

  20. 20

    Trigger a redirect to a page on form submission after three incorrect attempts in Javascript

  21. 21

    header() redirects page automatically instead of after submission of form

  22. 22

    How to show the success message of PHP in html page on Form submission

  23. 23

    Using jQuery AJAX to call a php page on form submission

  24. 24

    Why do I see a blank screen after form submission in PHP?

  25. 25

    PHP after form submission disappears instead of redirecting via header location

  26. 26

    How to show a success message after PHP form submission?

  27. 27

    Keep form data inside the field after submission using php

  28. 28

    how to clear form using php PDO after successful submission

  29. 29

    jquery ajax requests are not responding on the new tab after the php form submission

HotTag

Archive