Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Page redirect with post
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.



Report this thread to moderator Post Follow-up to this message
Old Post
Andrew C.
03-27-04 04:13 AM


Re: Page redirect with post
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...




Report this thread to moderator Post Follow-up to this message
Old Post
Michael Vilain
03-27-04 04:13 AM


Re: Page redirect with post
> 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
}

?>



Report this thread to moderator Post Follow-up to this message
Old Post
DB McGee
03-28-04 04:53 AM


Re: Page redirect with post
Doh!

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;



Report this thread to moderator Post Follow-up to this message
Old Post
DB McGee
03-28-04 04:53 AM


Re: Page redirect with post

> 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.



Report this thread to moderator Post Follow-up to this message
Old Post
Andrew C.
03-28-04 04:53 AM


Re: Page redirect with post

> 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.



Report this thread to moderator Post Follow-up to this message
Old Post
Andrew C.
03-28-04 04:53 AM


Re: Page redirect with post
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.
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Greg Brewer
03-28-04 04:53 AM


Re: Page redirect with post
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.



Report this thread to moderator Post Follow-up to this message
Old Post
DB McGee
03-28-04 04:53 AM


Re: Page redirect with post
 
>
> That's exactly right.

Fantastic! Thanks again! :)

A.



Report this thread to moderator Post Follow-up to this message
Old Post
Andrew C.
03-28-04 04:53 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:59 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.