Forum

Thread tagged as: Question, Addons, Forms

Forms App validation and external script

Hello,

I want to make use of Perch's Form App validation. I'm still not clear on how the form is processed, but what I would like to do is after the form has been successfully completed using Perch validation, I want to pass along the form values to an external script.

My goal is to use the external script to format an email message that will be sent to a specified employee and send a copy to their manager, as well as a confirmation email to the website visitor.

I was thinking I could do this using the Redirection setting in the Forms App, but I'm not sure how it works. How would I pass the values from my form to the redirection specified page?

I am using the Forms App with the following template:

<perch:form role="form" id="contact" class="contact-form" method="post" app="perch_forms">

    <div class="form-group">
        <perch:label for="name"><i class="fa fa-fw fa-user"></i> Name</perch:label>
        <perch:input type="text" class="form-control" name="name" id="name" required="true" label="Name" />
        <perch:error for="name" type="required">
            <p class="help-block"><i class="fa fa-times-circle"></i> Please include your name</p>
        </perch:error>
    </div>

    <div class="form-group">
        <perch:label for="email"><i class="fa fa-fw fa-envelope"></i> Email Address</perch:label>
        <perch:input type="email" class="form-control" name="email" id="email" required="true" label="Email Address" placeholder="you@company.com" />
        <perch:error for="email" type="required">
            <p class="help-block"><i class="fa fa-times-circle"></i> Please include your email address</p>
        </perch:error>
        <perch:error for="email" type="format">
            <p class="help-block"><i class="fa fa-times-circle"></i> Please include a valid email address</p>
        </perch:error>
    </div>

    <div class="form-group">
        <perch:label for="phone"><i class="fa fa-fw fa-phone"></i> Phone Number</perch:label>
        <perch:input type="tel" class="form-control" name="phone" id="phone" required="true" label="Phone Number" />
        <perch:error for="phone" type="required">
            <p class="help-block"><i class="fa fa-times-circle"></i> Please enter a valid phone number</p>
        </perch:error>
    </div>

    <div class="form-group">
        <perch:label for="subject"><i class="fa fa-fw fa-question-circle"></i> Subject</perch:label>
        <perch:input type="text" class="form-control" name="subject" id="subject" required="true" label="Subject" />
        <perch:error for="subject" type="required">
            <p class="help-block"><i class="fa fa-times-circle"></i> Please include a subject for your message</p>
        </perch:error>
    </div>

    <div class="form-group">
        <perch:label for="message"><i class="fa fa-fw fa-pencil-square-o"></i> Message</perch:label>
        <perch:input type="textarea" class="form-control" name="message" rows="5" id="message" required="true" label="Message" />
        <perch:error for="message" type="required">
            <p class="help-block"><i class="fa fa-times-circle"></i> Please include your message</p>
        </perch:error>
    </div>

    <div class="form-group">
        <perch:label for="file"><i class="fa fa-fw fa-paperclip"></i> Add an Attachment</perch:label>
        <perch:input type="file" id="attached" accept="" />
        <p class="help-block small"> - Accepted formats: TXT, PDF, DOC, DOCX</p>

        <perch:error for="attached" type="filetype">
          <p class="help-block"><i class="fa fa-times-circle"></i> Please make sure that your file is in one of the accepted formats.</p>
        </perch:error>

        <perch:error for="attached" type="fileupload">
          <p class="help-block"><i class="fa fa-times-circle"></i> Couldn't upload your attachment - could be too big.</p>
        </perch:error>
    </div>

    <div>
        <perch:input type="submit" id="submit" value="Send Message" class="btn btn-default btn-lg" />
    </div>

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

Also, how do I specify the accepted formats for the file upload field? I have the ones I want in all caps, but I'm not sure if I can just list them the same way or format (txt, pdf, doc, docx) using the accept="" attribute.

Finally, I don't want to display an email address in my form if an employee is specified on the contact page through the URL variable. See currently, an employee can be specified to receive the form message, if their Perch _id value is passed through the URL (eg. /contact.php?u=23). This ID value is intended to be used in an external script to locate their email address within the Perch database. My question is, if I use the Redirection setting and am able to send my form values to an external script, then can I use PerchSystem to pass the URL _id value into a hidden field on my form so that it submits with the form to the external script?

           if(perch_get('u')) {
                $idFinder = perch_get('u');   
                PerchSystem::set_var('person-e', $idFinder);
            } 

Hope I'm making sense.

Thanks, Joshua

Joshua Rodriguez

Joshua Rodriguez 2 points

  • 7 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

Redirecting will just throw all of the values away because that's what redirection does.

I would make my custom script a perch app and then make use of the fact that you can submit your form to multiple apps. There is an example of doing that here https://grabaperch.com/blog/archive/add-a-blog-commenter-to-a-mailchimp-list

Don't let the fact we're talking about "an app" overcomplicate things. A Perch App can really just be a script it doesn't need to be a fully developed App as with our first party apps.