Contact Form 7: Conditional Redirecting

Redirecting without a condition

When you use the WordPress plugin “Contact Form 7” you can redirect the user to another page after submitting the answers by the follwing code:

on_sent_ok: "location.replace('');"

The line of code you have to copy into the “settings” box at the end of a specific form you created.

Redirecting on a condition

If you want to make the redirecting depending on a specific answer, you can use the following code:

on_sent_ok: " if (document.getElementById('car').value=='yes') {location.replace('')}
else { location.replace('') } "

The code in bold letters has to be changed by your settings.

For example: The question with the id “car” has two possible answers: “Yes” or “No”. If a person selects “Yes” he or she should be redirected to “”. If “No” is selected, the user should be redirected to “”.

4 Gedanken zu „Contact Form 7: Conditional Redirecting“

  1. Hi, thanks for the code. Tried the code, but I’m still having a problem with the conditional redirect; both options linked to the 2nd URL.
    Radio Button choices: Paypal, Bank Transfer

    I put this code in the additional settings, but it always leads to Google page instead of Paypal.
    on_sent_ok: “ if (document.getElementById(‚paymentmode‘).value==’Paypal‘) {location.replace(‚‘)} else { location.replace(‚‘) } “

    Any fix? 🙂

  2. The conditional redirect code doesn’t work with Contact Form 7 version 3.4.1 . Keep getting „document.getElementById(…) is null“ error.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.