Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 1st, 2004, 12:23 PM
Registered User
 
Join Date: Apr 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default jsp:forward equivalent

I need to take form data parse it into hidden form fields and then send it off to another page. Is there a way to do it without having to push a submit button? Obviously I need a submit button on the form page but I need a second processing page. I don't want to use any javascript. Something in php similar to a jsp:forward. This could even be a dummy html tag that I don't know. My html skills are lacking.

Thanks,
Leo

 
Old April 3rd, 2004, 08:25 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I can't think of anything that can do this on the client-side without adding JavaScript.

On the server-side, OTOH, you could store data in session variables. Sessions allow data to persist across multiple connections.

Here's a quick tutorial:

<?php
    // page1.php
    session_start();

    $_SESSION['foo'] = 'Hello, world!';

    echo '<a href="page2.php?sid='.session_id().'">Go to the next page</a>';
?>

<?php
    // page2.php

    session_start();

    if (isset($_SESSION['foo']))
    {
        echo $_SESSION['foo'];
    }
?>

Basically any page that needs to access session data must make a call to session_start() at the beginning of the script. session_start() must be called before any output from the script, since it makes changes to the HTTP headers.

The session id must be passed between any page that requires sessions, you can do that with the get method, as I have above. Or with the post method. By default session_start() also outputs a cookie containing the session id (this is the HTTP header change I mentioned).

Sessions are stored in a file on the server, but within a script you may set them using assignment and unset them using unset(), as you would with any other variable.

Here's the PHP manual page on sessions:
http://www.php.net/session

If you need to see more examples check out google:
http://www.google.com/search?q=php+session+tutorial

hth,
: )
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old April 7th, 2004, 04:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not terribly familiar with JSP's jsp:forward command, but I gather it works in a manner similar to if you were to rewrite the $_POST array back into the header using PHP's header() function before adding the "header(location: page.ext);" directive.

That's another option, of course, but personally I agree with Richard: the use of sessions is almost certainly preferable. Passing around stuff in hidden fields is only really feasable while there are only a few values to pass around: and if there are only a few values to pass around, why not use a session to pass them around?

It is true that the broken system of session management that Microsoft used in ASP did a lot to make web developers steer shy of sessions, but PHP's system is much more robust.

Dan
 
Old April 7th, 2004, 04:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Another option is available if you don't need to explicitly make a new request (e.g., the page you're redirecting to is on the same server), you can also just include the PHP script you want to redirect to.


<?php // page_1.php

$some_var = "some value";

include ("page_2.php"); // executes page_2 with $some_var in global scope

?>

Bear in mind that sessions only work for the same site as well, so if you're posting form data to another site, then you'll need to use a client-side solution like javascript.

Generate an HTML form with all the appropriate values populated. Then have javascript submit the form in the onLoad event.

<body onLoad="javascript:document.myform.submit();">

<p>
  Please wait while you are redirected.
  If you are not redirected within 5 seconds, click
  <a href="javascript:document.myform.submit();">here</a>.
</p>
<form name="myform" method="post" action="some_other_page">
  <input type="hidden" name="whatever" value="some value" />
  ...
</form>
</body>


Take care,

Nik
http://www.bigaction.org/
 
Old May 19th, 2004, 03:16 PM
Registered User
 
Join Date: May 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

[quote]Originally posted by Daniel Walker
 ...rewrite the $_POST array back into the header using PHP's header() function before adding the "header(location: page.ext);" directive.

Daniel, can you give an example of a header command that rewrites $_POST array back in? This is exactly what I need to do. My initial attempts have failed...

Thanks,
Gail
 
Old May 19th, 2004, 04:18 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Quote:
quote:Originally posted by gailp
Daniel, can you give an example of a header command that rewrites $_POST array back in? This is exactly what I need to do. My initial attempts have failed...
From what I've read that isn't possible.

Here's a few URLs on the topic..
http://ppewww.ph.gla.ac.uk/~flavell/...-redirect.html
http://www.webmasterworld.com/forum13/598.htm
http://www.webmasterworld.com/forum88/2365.htm

hth!

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old May 19th, 2004, 07:17 PM
Registered User
 
Join Date: May 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by richard.york
 From what I've read that isn't possible.
Richard, thank you for those urls!!! It was driving me nuts not to understand what the problem was. Interesting to learn that POST data is not sent in the headers!

Gail





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to forward some action from javascritp msg2ajay Javascript 1 September 3rd, 2007 09:08 AM
jsp:forward problem rushman Pro JSP 1 October 24th, 2005 10:36 AM
Forward after Login ~Bean~ ASP.NET 1.0 and 1.1 Basics 1 July 14th, 2005 05:14 PM
Forward booking rahilahmed Pro VB Databases 1 June 16th, 2004 06:02 PM
Forward Booking rahilahmed Excel VBA 0 May 24th, 2004 09:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.