Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Adding a Contact Form Working with Get Variables

Steven Del Rio
Steven Del Rio
17,746 Points

Pressing the 'back' button after submitting the form

Hi all,

The code in this lesson works as intended for me. However I've noticed that when pressing the back button after submitting the form, it takes me back to the form again. As we learned in a previous lesson, this is bad practice because we wouldn't want the user to re-submit the same form. The current code does not seem to handle this correctly? I assume because isset($_GET["status"]) returns a false value at this point.

Any suggestions? Thanks

2 Answers

Jason Cullins
PLUS
Jason Cullins
Courses Plus Student 4,893 Points

I don't know about what the lesson says, but here is what i do.

Most of the time i use a session variable to let me know that the form has been submitted. So on the page the form takes them to after clicking submit, i will set something like this

<?php
session_start();
$_SESSION['formname_submit'] = true;

then if they click the back button in their browser, i have something like this at the top of my code on that page

<?php
session_start();

if ( $_SESSION['formname_submit'] === true )
{
    // enter logic here or send them to another page
    header("Location: /form_submit_thankyou_page.php");
    exit;
}

// rest of your code to show the form will be below here

Hope this helps!

Steven Del Rio
Steven Del Rio
17,746 Points

Thanks Jason. I don't believe the 'session_start(); ' was covered in the lesson. I think this solves my problem :)