Forum

Thread tagged as: Question, Discussion, Forms

Perch Forms

Hello Support.

I followed this tutorial in the following link below so that upon submitting the form on my website the page wouldn't refresh.

https://forum.grabaperch.com/forum/08-08-2014-forms-app-js-and-page-refresh

This is working fine and it is posting to the perch backend etc however I am wondering if it's possible to display validation error messages without refreshing the page?

Once the form submit I get a success message but it would be a nice touch to get the validation messages also if a field was filled in incorrectly.

Is this possible to do?

Many thanks for any help in advance.

Joshua McGinn Sheridan

Joshua McGinn Sheridan 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

The only way would be to submit the form via ajax and update the DOM accordingly.

Hello Drew

Thanks for your response - could you give me some more information regarding this / possibly point me in the right direction on how I would achieve this.

I have attached my code below -

Code for form

<perch:form id="get-quote-form" method="post" app="perch_forms" role="form" class="contactusform">

            <perch:label>Full Name:</perch:label>
            <perch:input id="name" type="text" required="true" label="Name" />
            <perch:error for="name" type="required">
            <span class="error">Please enter your full name.</span>
            </perch:error>

            <perch:label>Phone Number:</perch:label>
            <perch:input id="phone" type="text" required="true" label="Phone" />
            <perch:error for="phone" type="required">
            <span class="error">Please enter your phone number.</span>
            </perch:error>

            <perch:label>Email Address:</perch:label>
            <perch:input id="email" type="text" required="true" label="Email" />
            <perch:error for="email" type="required">
            <span class="error">Please enter your email address.</span>
            </perch:error>

            <perch:label>Town:</perch:label>
            <perch:input id="town" type="text" required="true" label="Town" />
            <perch:error for="email" type="required">
            <span class="error">Please enter your town/city.</span>
            </perch:error>

            <perch:label>Device:</perch:label>
            <perch:input id="device" type="select" required="true" label="Device" options=",Not Sure, ,iPhone 3G, iPhone 3GS, iPhone 4G, iPhone 4S, iPhone 5, iPhone 5C, iPhone 5S, iPhone 6, iPhone 6 Plus, ,MacBook, MacBook Pro, MacBook Air, ,iMac, ,iPad 1, iPad 2, iPad 3, iPad 4, iPad Air, iPad Air 2, iPad Mini 1, iPad Mini 2, iPad Mini 3, ,iPod Classic, iPod Mini, iPod Nano, iPod Shuffle, iPod Touch" />  
            <perch:error for="device" type="required">
            <span class="error">Please enter a valid email address.</span>
            </perch:error>          

            <perch:label>Message:</perch:label>
            <perch:input id="message" type="textarea" required="true" label="Message" />
            <perch:error for="message" type="required">
            <span class="error">Please enter your message.</span>
            </perch:error>    

            <perch:input type="submit" id="submit" class="submit" value="Send Form" />

            <perch:success>
            <script>
            return 'SUCCESS';
            </script>
            </perch:success>

</perch:form>

Script for submitting form without page refresh

<script> 
$('#form1_get-quote-form').submit(function(){ 
  $.ajax({
    type: "post",
    data: $('#form1_get-quote-form').serialize(),
    dataType: "html",
    timeout: 8000,
    cache: true
  }).done(function(data) {
    if(data.indexOf("success") > -1) {
   // code for success goes here
    $('#form1_get-quote-form')[0].reset();
    } else {
    // code for success goes here
    };
  }).fail(function() {
   // code for success goes here
  });
  return false;
});
</script>

Thanks for your help.

Drew McLellan

Drew McLellan 2638 points
Perch Support

How is it failing?

It's not failing at present. It's posting the form to the backend but no success message is appearing and no validation errors are appearing if an attempt is made to submit the form without filling in the all the fields.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to look at the HTML that is turned. Find the form, and then update the page with the new form HTML.

I turned off the HTML5 validation, and commented out the script to stop the page refreshing etc. and when I clicked submit the page refreshed but none the error messages appeared. I also checked the page source and I could see all the code for the form but not the perch:error tags. Any ideas why this is happening?

Drew McLellan

Drew McLellan 2638 points
Perch Support

If you turn on debug, do you get any validation errors being flagged?

When I submit the form with debug switched on I get the following -

Debug Message
SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
Array
(
    [name] => required
    [phone] => required
    [email] => required
    [town] => required
    [device] => required
    [message] => required
)
SELECT * FROM perch2_pages WHERE pagePath='/iphonerepair/iphone-repair.php' LIMIT 1
Using template: \templates\pages\attributes\seo.html
SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 ORDER BY pageTreePosition ASC
SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/iphonerepair/iphone-repair.php' LIMIT 1
SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-002', '000') ORDER BY pageTreePosition DESC
Using template: \templates\navigation\item.html
SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/iphonerepair/iphone-repair.php' OR regionPage='*' ORDER BY regionPage DESC
Drew McLellan

Drew McLellan 2638 points
Perch Support

Those are the required fields that need to be completed for the form to submit.

Any idea why the validation errors aren't being pulled through when the form is submitted with the fields empty.

Rachel Andrew

Rachel Andrew 394 points
Perch Support

I would suggest that you get a basic Perch form working without any JavaScript. You can then see what you need to implement in your script to replace that.