hey ppl. I have many submit buttons in a form. Each of them does its thing in different php pages. But when, for example, 'add' button goes to 'add.php' it does its work but stays there) how to redirect page back? thanks

Recommended Answers

All 4 Replies

There are 2 simplistic ways.

The first and most recommended is the header redirect:

header("Location: pagename.php");

That sends the header location to the browser, telling it to redirect itself to whatever you replace pagename with.

The second, less elegant way:

<meta http-equiv="refresh" content="2;url=pagename.php">

Where 2 is the number of seconds to wait before sending a refresh command to the browser and the url obviously being the page you want to redirect to.

The main problem I have with the second is that the page has to load before it even begins the countdown and witin that amount of time, if the user presses ESC or clicks the stop button, the countdown stops and the page does not redirect. The header sent by php is, as far as I've ever been able to manipulate, impossible to stop from redirecting as it's one of the very first things the browser receives.

However, the header refresh can be tricky to use. As it's a header, you cannot echo or display ANY kind of text or anything. If you decide to use it but cannot get it to work, just post it and we'll see what we can do.

header("Location: add.php");

I am not sure that is the one you want. Hope this help..!

Try using the following code at the point you want to redirect.

<script type="text/javascript">
window.location.href="<? echo $_SERVER ;?>";
</script>

$_SERVER contains the value of the referring page.

Also avoid using header function from PHP. It causes hell of a trouble...

thanks a lot. I guess header helped and don't have troubles with it yet:) But thank you all. It's a pleasure to ask something here:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.