Jump to content

Go to URL


Diegar
 Share

Recommended Posts

Is there a function, in PHP, that could tell the page to just go to another page?Currently, i have a page that has a form. You fill out the form and it goes to another page to process the information and stick it in the db. Then it goes back to the original page by using the <script type=\"text/javascript\">history.back();</script>"; line. Only issue with this, is that the form is still filled out.What i would like to see instead is that i could have the processing php file just send them to the form.php page, using PHP. Not go back, but just to it, thus clearing the form. Header() doesn't seem to work in this situation, cause i get errors, so looking for something more like "goto this URL" or something, with a little syntax.Any help would be greatly appreciated.. Thanks in advance!

Link to comment
Share on other sites

That's what header does. You need to send the header before you send any other text, you can't send a header after the headers were already sent, and any time you start sending output PHP will automatically send all necessary headers. So make sure you use header before you send any output. You should have all the PHP code in your script on the top of the file before you do anything with HTML. PHP gets executed first anyway, so put it first and send a header if you need to.

Link to comment
Share on other sites

That's what header does. You need to send the header before you send any other text, you can't send a header after the headers were already sent, and any time you start sending output PHP will automatically send all necessary headers. So make sure you use header before you send any output. You should have all the PHP code in your script on the top of the file before you do anything with HTML. PHP gets executed first anyway, so put it first and send a header if you need to.
Okay.. i think i understand that. So if there is output, the header first will change the page, but still display the output of the code, for instance, at the top of the new page?
Link to comment
Share on other sites

So if i have an output of say a javascript, giving an alert to say the item has been entered, how would i then be able to go to another page? The header won't work in that sense, cause it gives me errors, since there is output already. If i put the header in front of everything, it will not see the rest of my code, that enters it into the db, will it?

Link to comment
Share on other sites

it will render all the code that you wrote on the page, unless you write die() or exit() which will stop executing what it's wrote under the die() or exit().

header("Location: http://www.example.com/");if ($this == $that) {	echo "this will show";	die()	echo "this will never show";} else {	echo "but this will if $this != $that";}

I hope that help

Link to comment
Share on other sites

Lets assume your page has the content of

<html> <head>  <title>Wee!</title> </head> <body>Hi</body></html>

But before that you add

 <?php header("location:someOtherPage.html"); ?>

You will not see "Hi", or any of the rest of the HTML in this page(which we added the header to) because the moment that the header() call line is parsed in PHP it will automatically load someOtherPage.html, ignoring any content you have on it.Please, understand that PHP is run SERVER-SIDE, Javascript is run CLIENT-SIDE. SERVER-SIDE is run first, CLIENT-SIDE is run afterwards. If you want an alert after the information sent then change your js to

<script type="text/javascript"> alert('Added Successfully!');  window.location="someOtherPage.php";</script>

Link to comment
Share on other sites

So if i have an output of say a javascript, giving an alert to say the item has been entered, how would i then be able to go to another page? The header won't work in that sense, cause it gives me errors, since there is output already. If i put the header in front of everything, it will not see the rest of my code, that enters it into the db, will it?
If you want to display a message on a page and then redirect, you would use a meta refresh tag. Otherwise redirect to a page that you can display the notice on, but you can't display a notice and then use a location header. Pages that display information should not include a location header, if the browser sees a location header it will not render the page. Conversely, if you send a location header there's no point to continuing to send output. Do whatever housekeeping is necessary and then stop the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...