For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2004 > Page redirect with post









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Page redirect with post
Andrew C.

2004-03-26, 11:13 pm


Hi folks,

I wonder if any of you can suggest a way to redirect a browser to another
script (for which I would typically use 'header("Location: ...");') AND
provide 'post' arguments to it at the same time?

In other words, I want to simulate the effect of the following <form>, but
in an automatic/programmatic type way without the user intervention:

<form method="post" action="somescript.php">
<input type="hidden" name="somevar" value="someval">
<input type="submit" value="Submit">
</form>

I hope I've given enough information for you to know what I'm getting at.

Thanks in advance for any information.

A.


Michael Vilain

2004-03-26, 11:13 pm

In article <9aea86ee2f4cd0bba99f34e2a3ee8afa@news.teranews.com>,
"Andrew C." <remove.teranews@andrewcornes.com.remove> wrote:

> Hi folks,
>
> I wonder if any of you can suggest a way to redirect a browser to another
> script (for which I would typically use 'header("Location: ...");') AND
> provide 'post' arguments to it at the same time?
>
> In other words, I want to simulate the effect of the following <form>, but
> in an automatic/programmatic type way without the user intervention:
>
> <form method="post" action="somescript.php">
> <input type="hidden" name="somevar" value="someval">
> <input type="submit" value="Submit">
> </form>
>
> I hope I've given enough information for you to know what I'm getting at.
>
> Thanks in advance for any information.
>
> A.
>
>


It's my understanding that you can't pass arguments to a page via POST
from a URL. That's what GET is for. Since the header(URL); function
does a redirect to the browser, the only way the page can get arguments
is though a GET.

So, the short answer to your question is no.

--
DeeDee, don't press that button! DeeDee! NO! Dee...



DB McGee

2004-03-27, 11:53 pm

> It's my understanding that you can't pass arguments to a page via POST
> from a URL. That's what GET is for. Since the header(URL); function
> does a redirect to the browser, the only way the page can get
> arguments is though a GET.
>
> So, the short answer to your question is no.


Or you could pass them in a $_SESSION variable (the following is untested -
could be bugs, missing semi-colons etc.)

1. formpage.php

<?php

if( $_SERVER['REQUEST_METHOD'] == "POST" ) {

// Do validation (can also do validation on next page)

// If validations passes
if( $validated ) {
foreach ( $_POST as $varname => $varvalue ) {
$post_vars[$varname] = $varvalue;
}
session_start();
$_SESSION['post_vars'] = $post_vars;

// redirect to nextpage.php
header( "Location: nextpage.php" );
exit;
}
}
?>
<html>
<head>
</head>
<body>
<h1>Form Page</h1>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><label for="var1">Var1</label><input type="text" name="var1" id="var1"
value="<? echo $_POST['var1']; ?>"/></p>
<p><label for="var2">Var2</label><input type="text" name="var2" id="var2"
value="<? echo $_POST['var2']; ?>"/></p>
<p><input type="submit" name="submit" value="submit"/></p>
</form>
</body>
</html>


2. nextpage.php
<?php

// Grab the post vars
session_start();
$post_vars = $_SESSION['post_vars'];

// Now you can access the post vars
if ( $post_vars['var1'] == 'foo' ) {
// do something
}

?>


DB McGee

2004-03-27, 11:53 pm

Doh!

Just pass the entire $_POST array into the session variable instead of iterating
through it!

eg. change this:

foreach ( $_POST as $varname => $varvalue ) {
$post_vars[$varname] = $varvalue;
}
session_start();
$_SESSION['post_vars'] = $post_vars;

to this:

session_start();
$_SESSION['post_vars'] = $_POST;


Andrew C.

2004-03-27, 11:53 pm


> It's my understanding that you can't pass arguments to a page via POST
> from a URL. That's what GET is for. Since the header(URL); function
> does a redirect to the browser, the only way the page can get arguments
> is though a GET.


Thanks for replying.

I'm not looking for a way of, for example, making 'header()' do a 'post'
with a redirect. I'm looking for ANY way to give a browser a new page to
display (as 'header("Location: <URL>")' will), but for it to have $_POST
variables available to that new page when I get there (as '<form
method="post" action="<URL>">' with a '<input type="submit">' -- and user
intervention -- does) -- but WITHOUT the human intervention.

The underlying mechanism exists because that's precisely what happens when
using form/method/action, but I don't want to have to wait for the user to
provide the 'submit' for it to happen.

It may be that the answer to this is also 'no'... :)

A.


Andrew C.

2004-03-27, 11:53 pm


> session_start();
> $_SESSION['post_vars'] = $_POST;


Thanks for taking the trouble to give code.

So, at the 'receiving' end, I would just be able to use:

$_SESSION['post_vars']['example_arg']

where 'example_arg' is the name of one of the original $_POST variables?

(Sorry about the noob question, but I'm still feeling my way with PHP.)

A.


Greg Brewer

2004-03-27, 11:53 pm

I've given some thought about a problem I was having that was very similar.
I haven't actually tacked it yet but I was thinking I could do something
like that following at the start of the file
<?php
if ($_POST['somevar'] === "go")
include('...');
else
{
?>
......
<?php
}
?>

With your submit element enhanced with an onclick to a little JavaScript
program for change the value of your "somevar" element to "go".

Would that work???

Greg

"Andrew C." <remove.teranews@andrewcornes.com.remove> wrote in message
news:9aea86ee2f4cd0bba99f34e2a3ee8afa@ne
ws.teranews.com...
>
> Hi folks,
>
> I wonder if any of you can suggest a way to redirect a browser to another
> script (for which I would typically use 'header("Location: ...");') AND
> provide 'post' arguments to it at the same time?
>
> In other words, I want to simulate the effect of the following <form>, but
> in an automatic/programmatic type way without the user intervention:
>
> <form method="post" action="somescript.php">
> <input type="hidden" name="somevar" value="someval">
> <input type="submit" value="Submit">
> </form>
>
> I hope I've given enough information for you to know what I'm getting at.
>
> Thanks in advance for any information.
>
> A.
>
>



DB McGee

2004-03-27, 11:53 pm

Andrew C. wrote:
>
> Thanks for taking the trouble to give code.
>
> So, at the 'receiving' end, I would just be able to use:
>
> $_SESSION['post_vars']['example_arg']
>
> where 'example_arg' is the name of one of the original $_POST
> variables?
>



That's exactly right.


Andrew C.

2004-03-27, 11:53 pm


>
> That's exactly right.


Fantastic! Thanks again! :)

A.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com