• Resolved ramonesmania

    (@ramonesmania)


    I want to create a Survey with Contact Form 7 and Contact Form DB.
    This survey must filled just once for each User-IP.

    So when someone submits succesfully the form i want a cookie to be created. This cookie will handle the users that already have submitted the form and they will be redirected to another url.

    Can someone help me with that? Thanx in advance!

    https://wordpress.org/plugins/contact-form-7/

Viewing 11 replies - 1 through 11 (of 11 total)
  • You should be able to add a call to a javascript function in the on_sent_ok parameter. Something like adding this line to the Additional Settings box:
    on_sent_ok: "my function();"

    Unfortunately, it doesn’t look like you can pass form data through that function, but maybe my syntax is wrong. I tested:
    on_sent_ok: "console.log('Name: '+[your-name]+' Email: '+[your-email]);"
    gives me the error ReferenceError: Can't find variable: your

    So it looks like you’ll be able to create a cookie with general info, like the fact that the form was sent, but not with specific info, like the form data.

    See http://contactform7.com/redirecting-to-another-url-after-submissions/

    Change the code to
    on_sent_ok: "'my function(); location =http://example.com/';"

    Thread Starter ramonesmania

    (@ramonesmania)

    I’ve already done it!!

    To save the cookie after the user submit correct the form i’ve add the following code.

    on_sent_ok: "document.cookie ='cookiemname=allow;expires=Tue, 27 Sep 2015 10:40:41 UTC; path=/'; location = 'http://example.com';"

    To check if the cookie already exists to the user i add the following code at the theme file.

    <script>
    <!--
    
    // page to go to if cookie exists
    go_to = "http://www.example.com/";
    
    // number of days cookie lives for
    num_days = 360;
    
    function readCookie(cookieName){
        var start = document.cookie.indexOf(cookieName);
        if (start == -1){
            document.cookie = "cookiemname=yes; expires=" + ged(num_days);
        } else {
            window.location = go_to;
        }
    }
    
    readCookie("cookiemname");
    // -->
    </script>
    ashishsrii

    (@ashishsrii)

    Hi ramonesmania,

    I added following code in my contact form Additional Setting:
    on_sent_ok: “document.cookie =’cookiemname=allow;expires=Tue, 27 Sep 2015 10:40:41 UTC; path=/’; location = ‘http://mysite.com/thanks/&#8217;;”

    And following code in header file above </head> tag:

    <script>
    <!–

    // page to go to if cookie exists
    go_to = “http://mysite.com/thanks/&#8221;;

    // number of days cookie lives for
    num_days = 360;

    function readCookie(cookieName){
    var start = document.cookie.indexOf(cookieName);
    if (start == -1){
    document.cookie = “cookiemname=yes; expires=” + ged(num_days);
    } else {
    window.location = go_to;
    }
    }

    readCookie(“cookiemname”);
    // –>
    </script>

    But when I fill the form, it goes in loop and tries opening thanks page again and again.

    Can you please tell me where I am doing wrong? Thanks in advance!!

    Thread Starter ramonesmania

    (@ramonesmania)

    I assume that the form is in the thanks page that’s why it keeps redirects you there.

    The form should be in another page than the thanks.

    ashishsrii

    (@ashishsrii)

    Hi ramonesmania,
    I have removed the code as it was creating loop but my form is on the page http://realtycloud.ca/home/contact-2/ and the thanks page is http://realtycloud.ca/home/refer-earn-thanks/

    What should I do?

    Thread Starter ramonesmania

    (@ramonesmania)

    Oh I see. Probably you had the script in the header and it was called in every page creating that loop.

    You should create a new theme file just for the contact form in which you’ll have the script that checks for the cookie.

    I hope that will make it work.

    ashishsrii

    (@ashishsrii)

    Thanks a lot ramonesmania. It worked like charm!!
    You are great.

    I have some more forms in the website. Just want to know one more thing:
    “Do I need to create a new theme page for all the forms which need this functionality?”

    Thread Starter ramonesmania

    (@ramonesmania)

    You can create new theme page or you can use conditions to check the page and then run the proper script for each one.

    Something like this i think that will work

    <?php
        if(is_page( 'page' )){
            echo "
                <script>
                </script>
            ";
         }
      ?>

    ashishsrii

    (@ashishsrii)

    Thanks Ramonesmania 🙂

    ashishsrii

    (@ashishsrii)

    Hi Ramonesmania,

    Sorry to disturb you again, I just want to know “How to set cookie for 30 minutes only?”

    Means the user will be able to see form page after 30 minutes after filling form.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Redirect if User already has Sumbit the form’ is closed to new replies.