• I am trying to embed an if/else statement for the automated html message that goes out after a user submits the cf7 form on my site.

    What I am trying to accomplish is that if a user leaves the [first-name2] field empty the opening line of the message should say “Hey first name,” and if both fields are not null then it should say “Hey first name1 & first name2,”.

    For some reason the condition is not working. I am assuming that it is not understanding what “[first-name2]” generated tag is.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
    <html>
    <body>
    <?php if (empty([first-name2])) { ?>
         <h2><span style="color: #666666;">Hey [first-name1],</span></h2>
    <?php } else { ?>
        <h2><span style="color: #666666;">Hey [first-name1] & [first-name2],</span></h2>
    <?php } ?>
    </body>
    </html>

    I would greatly appreciate any help.
    Thank you!

    https://www.ads-software.com/plugins/contact-form-7/

Viewing 6 replies - 1 through 6 (of 6 total)
  • I’m pretty confident you can’t put PHP in the mail() section, as a security measure (just like you can’t use PHP in WP pages or posts, without a separate plugin).

    If you really want to do something like it seems, you’re going to need to do so on the back-end or with an additional plugin. Or, if you don’t wan to do that… you can just have them select whether there is a second recipient:
    For the form:

    <p>[text name1 placeholder "1st recipient"][text name2 placeholder "2nd recipient"]
    Is there a second recipient? [radio radio-298 "Yes|?and?" "No|"]</p>

    For your email:
    Hi [name1][radio-298][name2],
    It will render in the email:
    (no, with only 1st rec.) “Hi Bob,”
    (yes, with both rec.) “Hi Bob and Janice,”
    (yes, with 1st rev.) “Hi Bob and ,”
    (no, with both rec.) “Hi BobJanice,”

    So, it’s not fool-proof but if people follow your form correctly, it will turn out your desired result with minimal coding or having to find a way to open up the form to allow PHP in the email formatting. Just a thought/option.

    Thread Starter dandaman209

    (@dandaman209)

    Wow!! that worked. Thank you kylee6114! Your awesome ??

    I have another question now that kylee6114 has enlightened me to this unconventional way of solving my issue.

    In kylee6114’s example I found a way to hide “2nd recipient” from the user. What I would like is that, if the user answers “Yes” the “2nd recipient” placeholder should appear. And if the user answers “No” (or maybe… if they don’t answer at all) the “2nd recipient” placeholder should not appear at all.

    I found this forum that explains how to hide/show elements on a page. But using the [select] (drop down) option. I tried tweaking the function to have the placeholder appear when the user clicks the “Yes” radio button but to no avail. I am not sure how to do a boolean check for cf7’s radio button. See link below for show/hide tutorial.

    https://www.ads-software.com/support/topic/plugin-contact-form-7-this-is-how-to-showhide-fields-with-jquery?replies=44

    Thanks Guys!

    You’re welcome! Glad it offered you an acceptable substitute. =)

    Per your other question, have you tried to make the visibility of the whole 2nd recipient field be what’s contingent on the radio button and not just the placeholder? I’m thinking that’s your only option with the path you’re trying to go down. Plus, if you get it working like that, it will offer a really nice/clean/professional-looking result with there only being fields present that you want filled out. (And, hiding that entire element/tag when there should only be one recipient will help ensure the end result is desired.)

    Thread Starter dandaman209

    (@dandaman209)

    Yes, I did. And it rendered in the email as you explained it would.

    I don’t think I was clear in my previous post. I am talking about hiding and showing the actual field from the form. If the user selects the “Yes” radio button the element “2nd recipient” will appear on the form, and if “No” it will stay hidden from the user. I was able to initially have the field hidden, but my issue is getting it to reappear if/when the user answers “Yes”.

    Below is the function I used. But I need have work for a radio button as opposed to a list.

    /*! jQuery script to hide certain form fields */
    
    $(document).ready(function() {
    
            //Hide the field initially
            $("#hide1").hide();
    
            //Show the text field only when the third option is chosen - this doesn't
            $('#awesome').change(function() {
                    if ($("#awesome").val() == "Nope") {
                            $("#hide1").show();
                    }
                    else {
                            $("#hide1").hide();
                    }
            });
    });

    In the forum there was a link to a jquery script for a radio button. But how do you check for a boolean in cf7?

    HTML code:

    `<input type=”radio” name=”lang_anz” value=”1″> Yes<br>
    <input type=”radio” name=”lang_anz” value=”0″ checked=”checked”> No<br>
    <div id=”div_name”>Some text in div.</div>`

    Jquery script:

    `$(document).ready(function(){
    $(“:radio:eq(0)”).click(function(){
    $(“#div_name”).show(1000);
    });

    $(“:radio:eq(1)”).click(function(){
    $(“#div_name”).hide(1000);
    });

    });`

    I am talking about hiding and showing the actual field from the form. If the user selects the “Yes” radio button the element “2nd recipient” will appear on the form, and if “No” it will stay hidden from the user.

    We’re on the same page, ’cause that’s what I was (attempting) to talk about too. I think the 3.5 hours of sleep last night muddled my words. =)

    I doubt I’ll get around to it tonight but I’ll try to see what I can help with later this weekend (when I can think straight – ha!). That is, unless you or someone else figures it out first.

    Thread Starter dandaman209

    (@dandaman209)

    I found the link below. Not sure if it can be tweaked to fit my specifications.

    https://www.pinwire.com/sci-tech/contact-form-7-conditional-form/

    Thanks again.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Embed if/else Statement Within Message Body’ is closed to new replies.