3

I have 2 pages. After the user inserts all their info on page 1, they go to page 2. All the inputted information gets correctly stored in $_POST so I can access it in the next page. I can also insert into mysql database just fine no problem. The thing I'm trying to do is to ONLY execute the insert into the database code IF they click the submit button on the second page. I tried doing this:

Page2.php

<script>
$(document).ready(function(e) {
    $("#PaySubmit").click(function() {
        $.ajax({
            url: 'Insert.php'
        });
    });
});
</script>
<input type="submit" name="PaySubmit" id="PaySubmit" value="Continue"/>

Insert.php

try {
    $link = new PDO('mysql:host=****;dbname=****;charset=UTF-8','****','****');
    $first = $_POST["fname"];
    $last = $_POST["lname"];

    $stmt = $link -> prepare("INSERT INTO Conference (`First Name`, `Last Name`) VALUES (:first, :last)");
        $stmt->bindParam(':first', $first);
        $stmt->bindParam(':last', $last);
    $stmt->execute();

} catch(PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

The problem is, when you click submit on the 2nd page $_POST gets replaced with data from the second page, so I no longer have access to the 1st page's data. Any ideas on how to go about this?

3 Answers 3

5

Store all the values from the first post in the current SESSION.

You can then access them with the $_SESSION global.

Have a look at the serialize function, it might be able to help you a lot. Or json_encode. They are very useful for storing arrays in one variable and retrieving it later.

Sign up to request clarification or add additional context in comments.

6 Comments

I thought about doing this but is there any other solution? I didn't post all the code but i have a large amount of data to insert, and to put it all in the session is gonna be extremely tedious
It is not really that tedious try foreach ($_POST as $k => $v) { $_SESSION[$k] = $v; }
@MikeBrant nice one, perhaps thought it would be worth prepending something in the name not to overwrite other already set variables e.g. ` foreach ($_POST as $k => $v) { $_SESSION['post_'.$k] = $v; }
@MikeBrant Yah but I have some arrays inside $_POST that you have to access like $_POST["array"][0] so I don't know how the foreach would handle that. Is there a way just to straight up copy the entire post array into a session array in one go without loops?
@Richard Your nested array structure would remain exactly the same. For your example $_POST['array'][0] would become $_SESSION['array'][0] as $_SESSION would be set with the entire array stored in array key. To answer your question about how to do this without loops, you could also simply merge the arrays like array_merge($_SESSION, $_POST);
|
0

As an alternative to storing in session, you could simply pass the values from the first page to the second via use of hidden input fields. Something like this:

foreach($_POST as $k => $v) {
?>
<input type="hidden" name="<?php echo $k; ?>" value="<?php echo $v; ?>" />
<?php
}

Of course you may want to sanitize data in post in some manner before posting to next page, but this should give you an idea of what to do.

Comments

0

I'd suggest you make the DBconnection as a singleton class. And adjust the query once you go to page 2, and execute it, when you click submit in page 2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.