Had an issue recently with a university project where my team wanted to have a contact form on a webpage, to capture the usual contact info from website visitors. However, this would take us to the maximum number of pages allowed, and so, we could not have a “thanks, message sent” page.
Here’s how I did it:
- When the page containing the form loads, it checks if $_SESSION is set for form. If not, it
displays the form within the page, otherwise displays a custom message in the page – but
no form. - When the form is submitted, the script sets $_SESSION for the form and then reloads the
page. This has the added advantage that the form cannot easily be submitted umteen times.
Ok.Your form needs to contain something like this:
<?php
session_start(); //Set Session Data.
if (isset($_SESSION['form_name'])){ //this line checks if session id is set
//if session ID is set, form has already been sent so display custom message
echo ‘<br />Your email has been sent, thanks.<br /><br />’;
} else {//session ID not set so display form
//Put form HTML in here
}
?>
The form sends the data to a script called send_form.php, which looks like this:
<?php
//Set Session Data
session_start();//this line sends a cookie called PHPSESSID
//insert here script to validate & send data
$_SESSION['form_name'] = TRUE; // set session ID for the form
//Next, relaod the same page
print “<meta http-equiv=\”refresh\” content=\”0;URL=page.php\”>”;
?>
I tested it on the wireframe and it worked fine.