0

So I am building this "booking form" (really it just emails somebody) and I want the form to be pre-populated with the user's details if they are logged in. I have this mostly working, but the email address will not populate from the global {email} variable.

Because of the parse order I have the form in an include and am embedding it in my template, passing the variables to it.

Here is the embed code:

{embed="/includes/.booking-form"
        first-name="default_value='{exp:member:custom_profile_data}{first-name}{/exp:member:custom_profile_data}'"
        last-name="default_value='{exp:member:custom_profile_data}{last-name}{/exp:member:custom_profile_data}'"
        email="default_value='{email}'"
        school="default_value='{exp:member:custom_profile_data}{school}{/exp:member:custom_profile_data}'"
    }

And here is the form code:

{exp:freeform:form form_id="1" required="first_name|last_name|email|school" return="{page_url}" inline_errors="yes" form:class="booking-form"}
{if freeform:general_errors}
    <h2>There were some error(s) with your submission:</h2>
    <ul>
        {freeform:general_errors}
        <li>{freeform:error_message}</li>
        {/freeform:general_errors}
    </ul>
{/if}
<p>
    <label><span>*</span> First name:</label>
    {freeform:field:first_name {embed:first-name}}
    {if freeform:error:first_name}
        <span class="error">
            {freeform:error:first_name}
        </span>
    {/if}
</p>
<p>
    <label><span>*</span> Last name:</label>
    {freeform:field:last_name {embed:last-name}}
    {if freeform:error:last_name}
        <span class="error">
            {freeform:error:last_name}
        </span>
    {/if}
</p>
<p>
    <label><span>*</span> Email:</label>
    {freeform:field:email {embed:email}}
    {if freeform:error:email}
        <span class="error">
            {freeform:error:email}
        </span>
    {/if}
</p>
<p>
    <label><span>*</span> Your school:</label>
    {freeform:field:school {embed:school}}
    {if freeform:error:school}
        <span class="error">
            {freeform:error:school}
        </span>
    {/if}
</p>
<p>
    <label>Comments:</label>
    {freeform:field:comments}<br />
    {if freeform:error:comments}
        <span class="error">
            {freeform:error:comments}
        </span>
    {/if}
</p>
<p class="submit">{freeform:submit}</p>
{/exp:freeform:form}

As I said, the first name, last name and school are pre-populating perfectly, it is only the email that is not working. I have done a bit of debugging, if I put the {embed:email} tag in elsewhere in the code it outputs correctly default_value='email@email.net', and if I manually type in the email into the embed code like so: email="default_value='email@email.net'" it pre-populates.

Anyone have any idea what's going on? Thanks in advance!

0

According to the ExpressionEngine Parse Order documentation, {email} is parsed later than embeds, as a standard global variable. This is a common issue with other variables such as {member_id} and {group_id}. One way around this is to use PHP and EE's session class to get the email. This will parse before the embeds, which should send a parsed email variable.

In other words, turn on PHP and try this in your {embed="/includes/.booking-form" ...} tag:

email="default_value='<?php echo ee()->session->userdata["email"]; ?>'"

Since you're using default="" in all {freeform:field:my_field} fields, you could also try sending only the email address and put default="" in {freeform:field:my_field}, i.e. use this in your embed instead:

email="<?php echo ee()->session->userdata['email']; ?>"

(Note that I'm being careful with the types of quotes used to avoid escaping issues). Then, in your form, use:

{freeform:field:email default_value="{embed:email}"}
| improve this answer | |
0

If you run just your embed template stand alone, does it return the results you are expecting. You may have to remove the "." in front of it. Try renaming your variable to user_email so there is no potential conflict.

| improve this answer | |
  • Hi, Thanks for replying. How can I run the template as a standalone if I need to pass variables to it? I tried renaming but it still doesn't work as user_email="default_value='{email}". The thing I find really strange is that user_email="default_value='hello@joebeaver.co.uk'" works. Confusing! – Joe Beaver Aug 28 '13 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.