如何在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?

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.

On the formular target page set:

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.