Forum

Thread tagged as: Question, Discussion

Using template for contacts

Hi, I've created a content template to display staff details, this includes email addresses. What I'd like to do is not display the email address on the page and instead use the 'id' of the contact to grab the email address out of the database and use on a form to email the member of staff. What's the best approach?

Jon H

Jon H 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

How are you handling the form submission?

Jon H

Jon H 0 points

I was going to use normal php code to handle the forms as I have no experience of the forms add-on.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Content tags are parsed before form tags, so you should be able to generate the options of a form field using regular content tags.

<perch:input id="send_to" type="select" options="<perch:content id="people" type="dataselect" page="/people.php" values="_id" options="first_name, last_name" />, " />
Jon H

Jon H 0 points

Not sure what you mean Drew? I have a page that lists the staff and on that page I want a link like 'Send Jon a message' that contains the id of the person (contact.php?id=3 for example). This then loads the email address from the database and uses a form and php to send the message.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, yes that would work.

$person = perch_content_custom('People', [
    '_id' => perch_get('id'),
    'skip-template' => true,
]);
Jon H

Jon H 0 points

Hi Drew, is it possible to dynamically set the to email address in the form add-on? I'd like to use the add-on using one form and passing an id, retrieving the email address as above, then setting the to email address in the form? Can this be done or am I best sticking with php?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you can do that - see the instructions in the sidebar when configuring the form options.

Jon H

Jon H 0 points

Hi Drew, not sure what you mean? How would you'd pass an email address retrieved from the database in the form options? Would like to use one staff form template and pass the email address when submitting the form not through the form options? Here's my form template...

<perch:form id="form-staff-contact" method="post" app="perch_forms" role="form">
    <div class="col-md-6 margin-15">
        <div class="form-group">
            <perch:input id="name" type="text" required="true" label="Name" class="form-control input-lg" placeholder="Name*">
        </div>
        <div class="form-group">
            <perch:input id="email" type="email" required="true" label="Email" class="form-control input-lg" placeholder="Email*">
        </div>
        <div class="form-group">
            <perch:input id="phone" type="text" required="true" label="Phone" class="form-control input-lg" placeholder="Phone*">
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-group">
            <perch:input id="message" type="textarea" required="true" label="Message" class="form-control input-lg" placeholder="Message">
        </div>
    </div>
    <div class="col-md-12">
        <perch:input id="submit" type="submit" class="btn btn-primary btn-lg pull-right" value="Submit">
    </div>

    <perch:success>
        <div class="alert success">
            <perch:content id="success" type="textarea" label="Thank you message" textile="true" editor="markitup" />
        </div>
    </perch:success>
</perch:form>

I want to send the info entered using this form to different staff members depending on '_id' of the member of staff (passed to the page using a querystring).

Drew McLellan

Drew McLellan 2638 points
Perch Support

The Forms app won't retrieve an email address from the database for you, but you can dynamically use any field in the form as per the instructions within the app.

Jon H

Jon H 0 points

I can't see anywhere in the instructions where it would allow you to dynamically change the to email address? Think we may have our wires crossed. To recap, I have a template that is used to enter staff details. I have a separate template that displays these details but doesn't display the email address. Instead there is a link that passes the id of the staff member to a contact page...

/our-staff/contact/?id=19

On the contact page I use a form. I can retrieve the email address using the id from the database what I'm struggling with is how I then change who receives the info when the form is submitted. I can see that you can change the to email address in the form options but what I can't see is how this could be changed dynamically? Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

The Forms app won't retrieve an email address from the database for you.

Jon H

Jon H 0 points

Okay. So if I wanted to use Perch CMS to store staff details and use an online form to send messages as described above what method would you recommend? Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are all the email addresses form the same domain?

Jon H

Jon H 0 points

Yes. The workaround I have in place at the moment is using php to send the message but I like the way the forms app stores content within the CMS.

Drew McLellan

Drew McLellan 2638 points
Perch Support

So you can dynamically use any of the fields from the form to send to. If you put the username in the form as a hidden field, you can then send the form to, e.g.

{email}@example.com