在PHP中,切换到另一个页面,然后切换到当前页面,而不会丢失当前页面中的字段值

I have created a form receipt.php in which data is fed in a normal way. After reaching the field called mode of payment, two radio buttons are provided:

1.DD/cheque 2.E-transfer

On clicking the radio1, dd_detail.php should appear and on clicking radio2, e_trans.php should appear.

After filling the dd or e-trans details, I need to click on "ACCEPT" button. Then I should get that previous page receipt.php without losing any filled values.

The problem is after clicking on ACCEPT button, the field values of receipt.php page which I have entered are empty....!!!

What to do for this?

PHP offers session support. It's cookie-based and temporary data is saved on the server. To get started, your first line in PHP needs to be <?php session_start(); ?>

Note that it has to be the very first thing in the PHP program - NOTHING, including spaces, can be shown before it, otherwise this will throw an error or wont work.

After that, you can read from / write to a special $_SESSION array. For example, beginning of your PHP file might have this:

<?php
session_start();

// if user clicks submit, save this for later
if( isset( $_POST['username'] ) ) $_SESSION['username'] = $_POST['username'];
?>

Now, the form field itself should be optionally pre-populated:

<input type="text" name="username" value="<?php echo $_SESSION['username'] ?>" />

If your data is fairly manageable, you can dump the entire $_POST variable somewhere into $_SESSION. For example:

<?php if( !empty( $_POST ) ) $_SESSION['data'] = $_POST; ?>

After that you could refer to above-saved field as $_SESSION['data']['username']. It's up to you to rework this into your workflow.

Why don't you store them in some $_SESSION variable, and then retrieve them from the second script?

e.g. :

// First script
$_SESSION['myfield'] = $value;

// Second script
$value = $_SESSION['myfield'];