Jump to content

How to send the form data from first page to the 3rd page?


ivytony

Recommended Posts

My website has two steps for submitting a url. The first step is just having users type in a target url (say: http://www.phpfreaks.com/forum), as soon as the user clicks on 'submit' button, (s)he will be redirected to a transition page (2nd page) that shows 'We are crawling on the target website url'. This transition page actually does a lot of work in the background:

 

1. Get the target webpage title

2. Get the images on target url (the $images is an array on the transition page)

3. Get keywords

4. content summary.

 

The 2nd page shall be able to redirect to the 3rd page automatically which requires user's confirmation for submission. I am wondering how to sent the variables obtained on the 2nd transition page to the 3rd page. Can I have the variables stored in a form on 2nd page and the 3rd page will get the variables by calling $_POST['variable']? But I don't know how to have a form submit automatically. Can someone shed me a light on it??

 

Thank you!

Link to comment
Share on other sites

Well depending on what is going on, you can either store them in the $_SESSION variable (just make sure you clear it when you are done), or on the 2nd page use hidden elements ie

 

<input type="hidden" name="first_page_variable" value="{$_POST['first_page_variable']}">

 

Then on the third page, $_POST['first_page_variable'] will be available for you.

Link to comment
Share on other sites

Well depending on what is going on, you can either store them in the $_SESSION variable (just make sure you clear it when you are done), or on the 2nd page use hidden elements ie

 

<input type="hidden" name="first_page_variable" value="{$_POST['first_page_variable']}">

 

Then on the third page, $_POST['first_page_variable'] will be available for you.

 

I prefer to use hidden input text fields, because I don't need to worry about cleaning sessions. So for a value in hidden text field to be submitted to next page, I don't need to have users click on 'submit' button??? -- this is something I don't know.

Link to comment
Share on other sites

OK, you have two different questins in this post - which confuses things.

 

The first issue of getting values from page 1 to page 3 has been taken care of (use either hidden fields or session data).

 

The second issue is how to get page 2 to submit itself. But, after looking at your explanation above I think you are looking at this wrong. According to your explanation page 2 will have a lot of back-end processes, but there is no user interaction. So, why would you need a form to submit?

 

I would suggest this. The user enters the URL on page 1 and submits to page 2. Page 2 goes through all of the backend functionality and gathers the additional data and does validation. If validation fails, then an appropriate error is displayed. If validation passes, then INCLUDE page 3. That way all the values available and created on page 2 are available on the included page 3. Page 3 will complete the operation.

Link to comment
Share on other sites

Thanks mjdamato!

 

sorry about the confusion. Yes, there is no user interaction with page 2. I was thinking to have page 2 submit the data itself by using forms . It seems impossible to submit form data without user's interaction, right??

 

I am not sure if I understand your suggestion correct. Do you mean this:

 

if (validated) {

include('page3.php');

}

else {

echo 'validation failed';

}

 

This way, I don't even need to use $_POST['variable'] to get the data processed at the back end, right?

 

Instead of redirecting to page3.php, page 2 includes page3? Is this what you mean??

 

Thank you so much ;)

Link to comment
Share on other sites

Yes, that is what I mean. Technically, you don't even need a page 3, you could simply stick it all on page 2. But, keeping different parts of functionality on different pages is good practice in my opinion. You would use the POST value for the URL the user submitted, but there would be no need to include it in another form.

 

You *can* use javascript to have a form auto-submit. But, that would be very bad practice in my opinion for many reasons.

Link to comment
Share on other sites

Hidden inputs wont work because they need for a form to be submitted (as someone else has already said).

 

Cookies/sessions is going to be one choice for your most streamlined code here. Put the information into cookies/sessions and then after that information has been used on page 3, delete the cookie/session. The disadvantage to this method is that if a user has cookies turned off, it wont work. But almost everyone leaves on cookies these days.

 

The other option is to pass the necessary information in $_GET variables. As long as its not too much information, this can be done:

 

page1.php

<form action="page2.php" method="post">
   <input type="text" name="textfield" />
   <input type="submit" value="submit" />
</form>

 

page2.php

<?php
   session_start();

   header('Location: http://www.example.com/page3.php?variable1=' . $_POST['textfield']);
?>

 

page3.php

<?php
   $variable1 = $_GET['variable1'];

 

This is your best bet as long as the amount of information you are passing isn't too much, and isn't too sensitive. If its lots of information, well how much of that information is required to be passed? Can you maybe pass a few keywords and (re)build the text on page 3? If its sensitive, use cookies, as any information sent using $_GET will be visible in your headers. So no passwords or anything.

Link to comment
Share on other sites

actually, I do need page 3 which is a confirmation for the submission.

 

Yes, you need a page 3, but there is no need to pass the variables from page 2 using POST, GET, or SESSION data. Unless page 3 will be used as a stand-alone page to receive data from other pages (not just page 2) then it only needs to be included.

Link to comment
Share on other sites

He originally asked for a way to get to page 3, and I gave one. Your method will work, but sometimes may not be what the person is looking for. No need to get snappy.

 

The easiest way to clear a session variable is just to set it equal to nothing:

 

$_SESSION['variable1'] = "";

 

But the variable will still exist, it just wont have a value.

 

You can also destroy the session:

 

session_destroy();

 

This will remove the session from the computer altogether (warning - ALL session variables are gone this way)

Link to comment
Share on other sites

No.

 

$_SESSION variables aren't stored on the server, they are stored on the user's computer. Each time the user accesses a page, if a $_SESSION exists on the users computer, it is sent to the server. If/when any changes are made, they are sent back to the users computer. So the  $_SESSION values are never stored on the server, even though they are used there.

 

If you set the value of the session to be nothing ($_SESSION['name'] == ""), the variable remains on the users computer, but has no value. If you destroy the session, it wont exist on the users computer anymore.

Link to comment
Share on other sites

page2.php

 

<?php
   session_start();

   header('Location: http://www.example.com/page3.php?variable1=' . $_POST['textfield']);
?>

 

But I want page2 to redirect by using this:

<META http-equiv="refresh" content="5;

            URL=http://www.example.com/page3.php?variable1=<?php $_POST['textfield']?>>

Because I want to page2 pause for processing the data for 5 seconds before redirecting to page3. Can I use this META refresh redirection on page2?

Link to comment
Share on other sites

Why do you need to pause at all? Page 2 won't finish unti it's done. By your explanation you are going to read the content provided by the URL and parse the data. You don't need to implement any type of pause. Simply have the page do it's processing and then include page 3. You are really making this more complicated than it should be.

 

Just have your processing done at the beginning of the page and then include page 3 at the end. The inlcude can't occure until the processing is done. Having page 2 as an intermediary page is pointless since it won't be sent to the browser till it's done processing anyway!

Link to comment
Share on other sites

Hidden inputs wont work because they need for a form to be submitted (as someone else has already said).

 

Cookies/sessions is going to be one choice for your most streamlined code here. Put the information into cookies/sessions and then after that information has been used on page 3, delete the cookie/session. The disadvantage to this method is that if a user has cookies turned off, it wont work. But almost everyone leaves on cookies these days.

 

The other option is to pass the necessary information in $_GET variables. As long as its not too much information, this can be done:

 

page1.php

<form action="page2.php" method="post">
   <input type="text" name="textfield" />
   <input type="submit" value="submit" />
</form>

 

page2.php

<?php
   session_start();

   header('Location: http://www.example.com/page3.php?variable1=' . $_POST['textfield']);
?>

 

page3.php

<?php
   $variable1 = $_GET['variable1'];

 

This is your best bet as long as the amount of information you are passing isn't too much, and isn't too sensitive. If its lots of information, well how much of that information is required to be passed? Can you maybe pass a few keywords and (re)build the text on page 3? If its sensitive, use cookies, as any information sent using $_GET will be visible in your headers. So no passwords or anything.

 

I tried this method, but my $newimages is an array generated on page2.php, which cannot be sent through this:

 

<?php
   session_start();

   header('Location: http://location/getimgs/page3.php?images=' . $newimages . ');
?>

Link to comment
Share on other sites

Can I have this icon ajax-loader.gif displayed to user when page 2 is processing data at backend?

 

Just put this image between the processing code and include page3?

 

thanks

 

If you want to do that then you will need to use AJAX like drewbee stated.

 

You would not be able to do that even with your plan of having page 2 self submit itself. Let's say it takes 10 seconds to process the data on page 2. Well, when the user submits page 1, page 2 will start processing the data and 10 seconds later the page will be delivered to the user. So, the user would see a white screen that entire time and then see the rotating image for about 1 second before the page self-submitted.

 

I would propose you first get it working without using AJAX. Then implement the AJAX later.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.