简体   繁体   中英

How to get post values from first page to third page ,as post

i have 3 pages

Page1.php,page2.php,page3.php

In page1.php, i have some hidden values, for example 'name'

After the submission of page1.php, it will go to page2.

Then after some process in page2.php, it should need to automatically submit to page3.php(where page3.php is in another sever)

Finally,when i print the $_POST variables in page3.php, i need to get the variable 'name'

You will want to look into sessions .

If you need them in POST, try this:

$display = "";
$saveFields = array('one', 'two'); // whitelist of fields to add to the form hidden
foreach ($_POST as $key => $val) {
    if (!empty($val) && in_array($key, $saveFields)) 
        $display .= '<input type="hidden" name="'.$key.'" value="'.$val.'" />';
}

echo $display;

Should get you where you want to go. The whitelist just ensure's that random stuff is not injected that does not need to be.

you could stick it in the session

<?php
session_start();
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}
?>

or you could pass them in hidden vars on page2.php if it has a form...

(1) Option is to add hidden input on page2 too.

(2) Option is to set the value from page1's name into session and use it on page3

There are several solutions:

  • PHP sessions
  • cookies
  • passing arguments as GET/POST parameters
  • storing data in database

In simple cases passing arguments as GET parameter page2.php?name=... or using hidden form field is the best solution

This seems straightforward to me, page one has a hidden value called name . Page 2 should retrieve the post $_POST['name'] and print it on page 2 as a hidden field. Once you post it to Page 3 you can retrieve it the same way $_POST['name'] .

Realistically if the data is exactly the same and is being carried all the way to page 3, why do you even need it? Can you not just declare it on page 3?

Okay, the way I read this is that on your first page you have a UI with a form. The form is then submitted for processing to page 2. After processing is done, you want to redirect, if you will, the user to another site (or server, doesn't necessarily have to make a difference).

If I got that right, here is what you should do; instead of using the header(); function (php), print an empty page with a hidden form with all of the details you want to send over and use javascript to emulate the user 'submitting' the form.

< div style="display: none;">
< form action="https://mywebpage.com/myscript.php" method=POST>
< input type=hidden name="key_1" value="value_1">
< input type=hidden name="key_2" value="value_2">
< input type=hidden name="key_3" value="value_3">
< input type=submit id="formButton" style="visibility: hidden; ">
< script language="javascript">
document.getElementById("formButton").click()
< /form>
< /div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM