I’m having a nightmare trying to create a variable (GET, POST or preferably SESSION) in an included file that can then be read by a control script when the included file exits (without ‘submit’). Nothing I’ve tried has worked so far, yet I can’t believe it’s not possible. I’m probably missing something small but vital.
In summary, I have an e-mail form which is called by the visitor clicking on a link which is a URL with query string (e.g. http://example.com/index.php?id=1234&name=myname). The control script filters the GET array, and loads the form. On submission the control script will reload the form (it still has the same query), which then checks for errors in the entries. If there are errors, the form is re-displayed. If there are no errors, the form script will extract e-mail addresses from MySQL, and send the e-mail(s) AND THEN EXIT (back to the control script). Everything works fine to that point.
The form action is ‘action=“”’, which returns the form complete with the original query string. I’m aware that ‘action=“?”’ would clear the query string, but then the control script wouldn’t return the form on submit (and, worse, I’d lose the data).
I thought it would be simple to set a SESSION variable if no errors were found, and read this variable on return to the control script, so that a ‘Success’ page could be shown. But the script doesn’t ‘see’ it, although it’s there when I view the session file in my editor.
There’s a lot of code involved, most of it working correctly, and nothing to do with the problem. Here are the bits I consider relevant:
Extract from control script (index.php):
if (isset($_GET['id']) && isset($_GET['name'])) {
if (isset($_SESSION['noerrors']) && $_SESSION['noerrors'] == "YES") {
$content = $_SERVER['DOCUMENT_ROOT'] . '/inc/msgsent.inc.htm';
include $_SERVER['DOCUMENT_ROOT'] . '/templates/pagetemplate.html.php';
unset ($_SESSION['noerrors']);
exit();
} else {
$_POST['action'] ='E-mail Shortlist'; // line 114
}
}
...
// SEND e-mail to Short-list (or single listing)
// This segement selects the e-mail address(es) from DB and presents the form for completion
if (isset($_POST['action']) && $_POST['action'] == 'E-mail Shortlist') {
$content = $_SERVER['DOCUMENT_ROOT'] . '/inc/mailformlogic.php';
include $_SERVER['DOCUMENT_ROOT'] . '/templates/pagetemplate.html.php';
exit();
}
The first block of code above SHOULD load the ‘Success’ page if the SESSION variable ‘noerrors’ is present, but this is the bit that doesn’t happen. ELSE it allows the script to continue to the second block, where the e-mail form is loaded.
The e-mail form itself (mailformlogic.php) has the following relevant parts:
if (isset($_POST['send'])) { //if the form has been submitted...
...check required fields and sanitise input...
if (count($errors) > 0) { // if there are errors
...re-display form with error messages, for correction...
} else { // send the e-mails
...prepare HTML and text e-mails...
...send e-mails...
$_SESSION['noerrors'] = "YES";
exit();
}
} // end of 'if form submitted'
// display the form either first time round, or if there are errors
...display the form for completion...
The script exits OK, but won’t display the ‘Success’ page, apparently because it can’t see the new SESSION variable ‘noerrors’.
I suspect it’s something to do with when the contents of the Session file are refreshed, as I can access other SESSION variables created earlier.
You can see all this at:
http://holidaymullandiona.com/index.php?id=2073&name=Maolbhuidhe+Bed+%26+Breakfast
Note: Actual sending of e-mails is disabled in this example.
Can anyone suggest what I need to do, please ?