set cookie after submit button in wordpress

I am not even sure if it is cookies I need here, perhaps there is a better method.

I currently have a page with links for downloads. To get to this page the user will have to submit their email address. I am using the plugin Contact Form 7 for the email entry. I then plan on redirecting this to the page. The plugin allows me to add IDs to the submit button.

Read More

Should I be setting a cookie here using JavaScript? I won’t be able to put JavaScript into the form, but I can have the form on my own page template.

Heres the outputted html, Im reluctant to go and try and change this as its a plugin..

<form action="/downloads/#wpcf7-f127-p124-o1" method="post" class="wpcf7-form" novalidate="novalidate">

<div style="display: none;">

<input type="hidden" name="_wpcf7" value="127" />
<input type="hidden" name="_wpcf7_version" value="3.4" />
<input type="hidden" name="_wpcf7_unit_tag" value="wpcf7-f127-p124-o1" />
<input type="hidden" name="_wpnonce" value="211b6bff7f" />

</div>

<p>Your Name (required)<br />

<span class="wpcf7-form-control-wrap your-name"><input type="text" name="your-name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" /></span> </p>

<p>Your Email (required)<br />

<span class="wpcf7-form-control-wrap your-email"><input type="email" name="your-email" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" aria-required="true" /></span> </p>

<p><input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit" /></p>

<div class="wpcf7-response-output wpcf7-display-none"></div>

Related posts

Leave a Reply

1 comment

  1. Use a $_SESSION variable to determine whether or not the user has set an email. Cookies can easily be changed or removed by the user. Of course, in this case you wouldn’t need to fear the latter, but you get the point. SESSION vars, on the other hand, are not going to be editable by any user of your application.

    Example HTML:

    <form method="post">
        <input type="email" placeholder="Enter email here..." name="email" />
    </form>
    

    Example PHP

    if(isset($_POST['email'])){
        // Set the session val
        $_SESSION['user_email_set'] = true;
    }
    
    // Check if user entered email
    
    if(isset($_SESSION['user_email_set']) && $_SESSION['user_email_set']){
        // Show downloads page
    }