Stefany Newman
Stefany Newman

Reputation: 524

Can't display verification errors on form submit in AMP

I am doing back-end data input verification with amp-form and I want to display the errors on the front-end. However, when I submit the form, even tho the form contains errors (no name inputted) the front-end says "Success!" instead of outputting the errors.

PHP code: https://gist.github.com/Stefany93/364db9e088b570fff83387494e8459a4#file-php

   action-xhr="scripts/contact_process.php" 
   verify-xhr="/scripts/contact_process.php"
   method="post" 
   target="_blank" 
   class="detailed_contact_form "
   custom-validation-reporting="as-you-go"
   >
      <fieldset class="user-valid valid">
         <legend>
            <span> Schedule an appointment</span>
         </legend>
         <div class="form-body">
            <div class="form-group">   
             <div class="form-group"> 
                  <div class="form-group"> 
               <label for="email">Email  </label> 
               <input type="text" required name="email" id="email"  value="exampleexample.com"class="form-input string-entry">
            </div>
               <label for="name">Name  </label> 
               <input type="text" required  name="name" id="name" value="Cindy"  class="form-input string-entry">
            </div>
            <div class="form-group">  
               <label for="phone">Phone </label>  
               <input type="tel" required name="phone" id="phone" value="123-345-6789" class="form-input string-entry">
            </div>
               <div class="form-group">  
               <label for="phone">Message </label>  
               <textarea name="message" required class="textarea" >I want to schedule an appointment for my dragon Skippy, for tomorrow 10AM</textarea>
            </div>
            <div class="form-group button-holder">
               <input type="submit" class="button" value="Send appointment!">
            </div>
         </div>
            <div verify-error>
        <template type="amp-mustache">
            {{#verifyErrors}}
                <p>{{message}}</p>
            {{/verifyErrors}}
            {{^verifyErrors}}
                <p>Something went wrong. Try again later?</p>
            {{/verifyErrors}}
        </template>
    </div>
    <div submit-error>
        <template type="amp-mustache">
            {{#verifyErrors}}
                <p>{{message}}</p>
            {{/verifyErrors}}
            {{^verifyErrors}}
                <p>Something went wrong. Try again later?</p>
            {{/verifyErrors}}
        </template>
    </div>

    <div submit-success>
    <template type="amp-mustache">
      Success! 
    </template>
  </div>

      </fieldset>

   </form> ```

Upvotes: 1

Views: 523

Answers (1)

Stefany Newman
Stefany Newman

Reputation: 524

I was getting a success message because AMP tracks HTTP headers and whenever I submit the form, if the submission was successful (even tho I couldn't send the email due to errors) the contact_process.php would return an HTTP header 2xx which then AMP will think it is a success.

submit-success will output on successful form submission, regardless if the form did what you wanted it to do.

submit-error will output whenever the back-end returns an HTTP header of 4xxx. In my case, I had to add this line of code:

header("HTTP/1.1 400 Bad Request");

Below the echo json_encode($error_array) and it worked!

Whenever you want to output errors, make sure to manually send an error HTTP header 4xx.

Upvotes: 2

Related Questions