My client has a "resource" blog with articles and PDFs that are available for download.
They would like for their users to enter in an email address before being allowed to download the PDFs (I know). The form would save the entry into the database and email an admin the information for a possible follow-up (I know..). They would also like for the user's data to be saved for that session so they don't have to fill out the form over-and-over if they choose to download other articles. They do not want the user to have to login or register.
I have this working outside of ExpressionEngine2 and Freeform, using $_SESSION variables to check if the form was submitted and saving the $_POST data into the $_SESSION array for auto-populating the form fields on subsequent article pages. The user just has to hit Submit again on the form, which is fine.
However, when implementing in EE 2.7.3 using FreeForm Pro 4.1.3, using the default form action, the POST and SESSION arrays are empty after submission. If I substitute the Freeform form:action it works as expected, but of course the form results aren't stored in the database or emailed to the admin.
They love the Freeform interface and ability to see and export previous entries. Also, I need to do the form submission check server-side, so a user can't just view the source and download the PDF without filling out the form.
Is there another way to accomplish this? I am a hook novice. Any help or insight is most appreciated.
Here's my current code on the blog listing template:
<?php session_start(); ?>
<!DOCTYPE html>
<head>
...
<body>
...
{exp:channel:entries channel="blog"}
<h1>{title}</h1>
<?php // SET VAR FOR FORM SUBMIT
$submit = ee()->input->get_post('submit', TRUE); ?>
<?php // IF FORM NOT FILLED OUT
if ( empty($submit) ) { ?>
<a class="download-prompt" title="Download Attachment" href="#"><strong>Download Attachment:</strong> {exp:channel:entries channel="blog"}{title}{/exp:channel:entries}</a>
<!-- Hidden Contact Form - Shown on '.download-prompt' Click -->
<div class="contact-form">
{exp:freeform:form
form_id="16"
required="email"
}
<div class="line">
<label>Email <span class="required_item">*</span></label>
<input type="text" name="email" id="freeform_email" placeholder="Email" value="<?php if (!empty($_SESSION['email'])) { echo $_SESSION['email']; } ?>">
</div>
<div class="line">
<input type="submit" name="submit" value="Submit">
</div>
{/exp:freeform:form}
</div><!-- /.contact-form -->
<?php // ELSE IF FORM IS FILLED OUT
} else if ( !empty($submit) ) { ?>
<?php // Save each POST value in SESSION
foreach ( $_POST as $key => $value ) {
${$key} = $value;
$_SESSION[$key] = $value;
}
?>
<p>Thank you message.</p>
<!-- Hidden iFrame to Auto Download Attachment -->
<script type="text/javascript">
$(document).ready(function() {
$('<iframe src="/resources/download.php?file_source={blog_attachment}"></iframe>').appendTo('body').hide();
});
</script>
<?php // ELSE SOMETHING BROKE
} else { ?>
<h3>Something went wrong. Please try again.</h3>
<p><a href="{page_url}">Try again.</a></p>
<?php } ?>
{/exp:channel:entries}