简体   繁体   中英

How to return on the same page after submitting a form in php.?

I am making an e-commerce website where I have lots of products. If a user goes to any product items page and submits any form there then they should come on the same page.

So, how to come on the same page?

在公式目标页面上设置:

header('Location: http://www.example.com/same_page');

Leave action attribute of form blank. Like so:

<form action="" method="post">

Or

<form action="#" method="post">

You can use this :

 header('Location: filename.php);

If you get any $_POST errors put it in a condition: if(isset[$_POST])

On your opening form tag add

action="submit.php"

  • then once it goes to that page when the submit button is hit add this to the bottom of that php page:

header("Location: success.html OR success.php");

Pass current url as a hidden input

<input name="redirect" value="PUT CURRENT URL HERE" />

into your view layer (if MVC). When the form is submitted, read the value like

$_POST['redirect']

and after doing what you have to do use header location as it was suggested. It's really simple. No rocket science.

If you want to submit various forms on same page and then go back to the page where the form is submitted, you must also send the form URL of the page where it was sent, preferably in a hidden element. And after processing form redirect to URL stored in hidden.

Thank You All. I Got My Answer

$_SERVER['REQUEST_URI']; will give the current URL with query strings.

Like my page is ' products.php?Product=20 '

echo $_SERVER['REQUEST_URI']; =>/products.php?Product=20

So, we can directly use this in header location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM