threxx
threxx

Reputation: 1243

PHP: Pass on form data to a third file without changing it in the second file

I want to pass on form1 data from file1.php to file3.php, then go to file2.php have a new form2 and then pass on the data from form2 to file3.php. However, so far I only have the data from form2 in file3 but I don't know how to pass on or get the data from form1 (file1.php).

file1.php with my form1 data

<form method="post" name="cartForm" action="file2.php?act=form">
    <div class="card-group">
        <div class="card">
            <img class="card-img-top" src="./images/10-11017B.jpg" alt="Card image cap">
            <div class="card-body">
                <h5 class="card-title">Basic Black Polo</h5>
                <p class="card-text">Word on the street is that 'black is the new black.'</p>
            </div>
            <div class="card-footer text-muted">
                <label class="card-text">Amount</label>
                <input type="number" step="1" value="1" class="form-control amount-input" name="amountProd1">
                <label class="card-text">Price</label>
                <input type="number" step="1" value="30" class="form-control amount-input" name="priceProd1">
            </div>
        </div>
    </div>
    <div class="checkout-button">
        <input type="submit" name="submit" class="btn btn-secondary" value="Checkout" />
    </div>
</form>

file2.php with form2 data (Here I have the data from form 1 - amountProd1)

<?php

    print_r($_POST['amountProd1']); //  1 is printed here
?>
<!-- end php code -->
<div class="container">
    <div>
        <form method="post" name="addressForm" action="file3.php?act=form">
            <div class="form-group">
                <label for="firstname">Firstname</label>
                <input type="text" class="form-control" name="firstname" id="firstname">
            </div>
            <div class="checkout-button">
                <input type="submit" name="submit" class="btn btn-secondary" value="Continue" />
            </div>
        </form>
    </div>
</div>

file3.php where I have the form2 data from file2.php but I no longer have the data from form 1 from file1.php:

<?php

    print_r($_POST['amountProd1']); // nothing is printed here
?>
<!-- end php code -->
<div class="container">
    <div>
        <form method="post" name="paymentForm" action="file4.php?act=form">
            <div class="form-group">
                <label for="payment">Payment</label>
                <input type="text" class="form-control" name="payment" id="payment">
            </div>
            <div class="checkout-button">
                <input type="submit" name="submit" class="btn btn-secondary" value="Continue" />
            </div>
        </form>
    </div>
</div>

Can anyone tell me how to pass on the form1 data in file2 so I can access them in file3?

Upvotes: 0

Views: 51

Answers (4)

KHIMAJI VALUKIYA
KHIMAJI VALUKIYA

Reputation: 626

file2.php

    <div class="container">
    <div>
        <form method="post" name="addressForm" action="file3.php?act=form">
<input type="hidden" value="<?php echo $_POST['amountProd1']; ?>" name="amountProd1">
<input type="hidden" value="<?php echo $_POST['priceProd1']; ?>" name="priceProd1">
            <div class="form-group">
                <label for="firstname">Firstname</label>
                <input type="text" class="form-control" name="firstname" id="firstname">
            </div>
            <div class="checkout-button">
                <input type="submit" name="submit" class="btn btn-secondary" value="Continue" />
            </div>
        </form>
    </div>
</div>

Upvotes: 0

Alexis
Alexis

Reputation: 149

You can use PHP $_SESSION to keep variables between forms

Exemple in file2.php :

<?php
session_start();
$_SESSION['amountProd1'] = $_POST['amountProd1'];

in file3.php :

<?php
session_start();
print_r($_SESSION['amountProd1']);

Upvotes: 1

Olawale
Olawale

Reputation: 751

You can save the details captured in the first form in the SESSIONS variable or use hidden inputs to retain the data on the second page, then forward it to the third page.

Then remember that all your page should have a session_start(); at the top of the page.

<?php 
session_start();

It is this call that gives each script access to the SESSION and the contents saved within it

Upvotes: 2

Nevermind23
Nevermind23

Reputation: 405

You can use hidden inputs

append form in data2 with:

<input type="hidden" value="<?php echo $_POST['amountProd1']; ?>">

and do same with data3 and finally all data will be sent to your file4.php

Upvotes: 1

Related Questions