Code Comments
Programming Forum and web based access to our favorite programming groups.
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.
Post Follow-up to this messageIn 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...
Post Follow-up to this message> 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
}
?>
Post Follow-up to this messageDoh!
Just pass the entire $_POST array into the session variable instead of itera
ting
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;
Post Follow-up to this message
> 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.
Post Follow-up to this message> 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.
Post Follow-up to this messageI'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.
>
>
Post Follow-up to this messageAndrew 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.
Post Follow-up to this message> > That's exactly right. Fantastic! Thanks again! :) A.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.