Forum

Thread tagged as: Question, Forms, Members

Using Perch Members User Details In Perch Forms

Is it possible to use the Perch Members functions like echo perch_member_get('email'); inside a Perch Form I would like to pre-populate some hidden fields in a form that will be within the secure members area. I want to know that the form has been completed by a particular member and not on behalf of another member by adding their user details in a non editable way.

Greg Riley

Greg Riley 1 points

  • 2 years ago
Simon Clay

Simon Clay 127 points

Hi Greg, yes, I think so.

You should be able to set the Member email as a variable and then pass it into the perch_form template.

Something like this:

<?php
//Get the member's email
  $member_email = perch_member_get('email');
//Set it as a Perch variable
  PerchSystem::set_var('this_member_email', $member_email);
//Show the form
  perch_form('form_template.html'); 
?>

Then in your form_template.html template:

<perch:input type="hidden" id="mem_email" value="this_member_email">

Hi Simon, Thanks for idea but I can't get it to work it.

In page declaration.php I have:

<?php
// Get the member's email
$member_email = perch_member_get('email');
//Set it as a Perch variable
PerchSystem::set_var('this_member_email', $member_email);
//Show the form
perch_form('declaration.html');  
?> 

then in declaration.html I have :

<perch:form id="declaration" method="post" app="perch_forms">

<perch:label for="name">Name</perch:label>
<perch:input type="text" id="name" required="true" class="form-control" placeholder="Name" label="Name" /> 

<perch:input type="hidden" id="mem_email" value="this_member_email">

<perch:input type="submit" value="Submit" class="btn btn-primary" />

<perch:success>
    <div class="alert alert-success">Sent OK</div>
</perch:success>

</perch:form>

The form displays on page fine and submits but when I look in perch for form responses it has the mem_email as this_member_email

looks like it is not seeing it as a variable but as text once it gets to the form.

Simon Clay

Simon Clay 127 points

Sorry, try this in your template:

<perch:input type="hidden" id="mem_email" value="<perch:forms id='this_member_email'>" />

That is outputting some junk to the page " /> so something not quite correct there. In the form results it still does not work but interestingly it is now leaving the mem_email value blank.

Simon Clay

Simon Clay 127 points

Hi, are you using Perch 2, or 3?

Either way, I think this should do it...

<perch:input type="hidden" id="mem_email" value="<perch:forms id="this_member_email" />" />

Fantastic that works perfectly. So glad to have that working, I have been thinking of other sites where this would come in handy.

I am using Perch 2.8.34 on this site, I really should upgrade it to Runway version 3.

Many thanks for your help.

Simon Clay

Simon Clay 127 points

That's great.

You can even give it a label to show in the responses eg.

<perch:input type="hidden" id="mem_email" label="Member Email" value="<perch:forms id="this_member_email" />" />

I often forget, when we're using Perch 2 we need to make sure we use XML-style self-closing tags in templates eg

In Perch 2 (or 3):

<perch:content id="heading" type="text" label="Heading" />

in Perch 3 only

<perch:content id="heading" type="text" label="Heading">

Most of the code examples in Perch docs show the new Perch 3 'HTML5' style tags.

I have added the labels which make it much neater, and extended it to submit full name, and an additional member ID number. Along with displaying the information that will be submitted at the top of the page. I would share a link except it is of course in a members only area.

Thanks once again for your help Simon.