|
-
Jan 5th, 2010, 09:41 PM
#1
Thread Starter
Member
[RESOLVED] Header redirect
I currently have an order page that has three functions:
1. Displays the order with a submit button
2. Redirects to a payment page
3. Displays results of the payment
For step 2 - I want it to redirect to the payment page, but it won't because there is html code before the header code.
I have tried moving the redirect code to the top before html but it still doesn't work. It does work if I move the call to the functions there as well but this of course means that 1. and 3. display up the top, not in my html template.
Is anyone able to help me out on this? ... currently it just refreshes and shows nothing within the template rather then redirecting. If I echo out $url its all there... just doesn't redirect.
Heres a simple code example:
PHP Code:
<?php //start the session and connect to db session_start(); ?> <html> <head> <title>Page title</title> <meta name="description" value="" /> </head> <body>
<?php //Functions if (isset($_REQUEST["result"])) { # this is a redirection from the payments page. print_result(); } elseif (isset($_REQUEST["submit"])) { # this is a post back -- redirect to payments page. redirect_form(); } else { # this is a fresh request -- display order with a submit button print_form(); } ?> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr> <td>Logo</td> </tr> <tr> <td> <?php function print_form() { //show order }
function print_result() { //show payment successful or not successful message } function redirect_form() { //redirect to payment page header("Location: ".$url); exit; } ?> </td> </tr> </table> </body> </html>
Last edited by buffy; Jan 5th, 2010 at 09:47 PM.
-
Jan 5th, 2010, 10:14 PM
#2
Re: Header redirect
all you need to do is check if the submit thing is set and then redirect at the beginning. you can do the rest of your stuff later. it doesn't all have to be within one big if/elseif statement.
PHP Code:
<?php session_start(); if(isset($_POST['submit'])){ //redirect header("Location: ...."); } ?> <html> <head> <title>My page</title> </head> <body> <?php //figure out what to do if(whatever){ print_result(); }else{ print_form(); }
?> </body> </html> <?php //function definitions ?>
and, like always, if you're not using both POST and GET, or either with COOKIES, then use $_POST, $_GET, or $_COOKIE instead of $_REQUEST. $_REQUEST['submit'] means that I can put the variable "submit" in your query string and I will be redirected. this is probably not the behavior you're looking for.
as a quick aside, it seems from the way you've set everything up that you are calling these functions OUTSIDE of your <table>, but you're defining them WITHIN your <table>. if you, therefore, think that where-ever you define your functions is the location that they are executed, let me assure you that that is not the case. this is also the reason why I formatted my example above the way I did. so, for example, the following function:
PHP Code:
function printStuff(){ echo "Stuff"; }
can be called anywhere in your script and will print "Stuff" where-ever it's called, not where it's defined. so, you can have a script like:
PHP Code:
<?php function printStuff(){ echo "Stuff"; } ?>
hello! this is my page about <?php printStuff(); ?>!
and it will print what you might expect: hello! this is my page about Stuff!
anyhow, I don't know if that was what you thought, but it was very alarming for me to see someone doing it the way you did it. it just seemed very, very odd.
oh. and one more thing. if you can, I'd use $_SERVER['REQUEST_METHOD'] to check whether or not a form was submitted via post, rather than checking if a form variable like "submit" is present. it's always been a small pet peeve of mine to see people naming their submit buttons when they have a basic form.
-
Jan 5th, 2010, 11:33 PM
#3
Thread Starter
Member
Re: Header redirect
Thanks kows.
To be honest the code is copied directly from the sample.php provided by our thirdy party payment hoster.
I wasn't sure what a better way would be to do the three actions... theres a bit of code also... so I left it as the sample to try and get it working.
(I must mention incase it means anything, their version had print <<<HTMLEOF HTMLEOF; which I removed and replaced with a curly bracket.)
Will change the redirect code first to get it working, then work on your other suggestions. Thank you!
Last edited by buffy; Jan 5th, 2010 at 11:38 PM.
-
Jan 6th, 2010, 12:01 AM
#4
Re: Header redirect
the "<<<" just means you're declaring a heredoc and then the HTMLEOF is just the heredoc's identifier. it's basically just another way of defining a string (rather than using quotes):
Code:
echo <<<EOF
my text goes here.
EOF;
$myvar = <<<EOF
my variable goes here.
EOF;
$myvar2 = <<<ANYTHING
the identifier can be any string.
ANYTHING;
read more about PHP strings.
-
Jan 7th, 2010, 05:07 AM
#5
Thread Starter
Member
Re: Header redirect
Hmm, it wasnt happy with the following (see below*) so will try your other suggestions as well...
Am not sure what the best way is so will alter my example to your one and see how that goes.
*example
PHP Code:
<?php session_start(); function redirect_form() { header("Location: http://www.google.com"); exit; } // end redirect form
-
Jan 7th, 2010, 02:12 PM
#6
Re: Header redirect
you don't really seem to know how functions work ;). you can define a function any where in your script. it doesn't matter. but PHP does not execute a function until it's called. you're not calling this function, you're defining it. define it SOMEWHERE else (generally, in an include file or at the bottom of a script), and then call it at the beginning of your script.
PHP Code:
<?php
session_start();
//redirect
redirect_form();
?>
<!-- do some stuff -->
<?php
function redirect_form()
{
header("Location: http://www.google.com");
exit;
}
?>
this script will start a session, then immediately redirect to google.com.
-
Jan 9th, 2010, 03:20 AM
#7
Thread Starter
Member
Re: Header redirect
Ahh thank you. Yes I don't use functions at all.
I didn't want to move all the if/elses there because I didn't want the others to execute there as well. So I just seperated the if statements and put just the redirect one up the top. Don't know why I didn't do that earlier.
And it works I also use this now instead:
PHP Code:
if($_SERVER['REQUEST_METHOD'] == "POST" && $_GET['Submit']=='yes')
Again, thank you.
-
Jan 9th, 2010, 10:56 AM
#8
Re: [RESOLVED] Header redirect
err. that will only work if you have a form action of something like "myscript.php?submit=yes," and you're submitting via POST. otherwise, the request_method would be POST and $_POST['submit'] would be the variable set.
also, if I'm understanding you correctly, you should have just been able to make the basic structure of your page like my first reply. the first redirect statement is at the top of the script, and the other two statements are found later on in the content.
either way, looks like you mostly have gotten it working now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|