Script and its AJAX Compatibility

Script β†’

// Wait for the DOM to be ready
$(function() {
  // Initialize form validation on the registration form.
  // It has the name attribute "registration"
  $("form[name='registration']").validate({
    // Specify validation rules
    rules: {
      // The key name on the left side is the name attribute
      // of an input field. Validation rules are defined
      // on the right side
      firstname: "required",
      lastname: "required",
      email: {
        required: true,
        // Specify that email should be validated
        // by the built-in "email" rule
        email: true
      },
      password: {
        required: true,
        minlength: 5
      }
    },
    // Specify validation error messages
    messages: {
      firstname: "Please enter your firstname",
      lastname: "Please enter your lastname",
      password: {
        required: "Please provide a password",
        minlength: "Your password must be at least 5 characters long"
      },
      email: "Please enter a valid email address"
    },
    // Make sure the form is submitted to the destination defined
    // in the "action" attribute of the form when valid
    submitHandler: function(form) {
      form.submit();
    }
  });
});

Source β†’

But this based on when an action of the form is normal, but I am using AJAX to submit form here.

My Ajax code is like this β†’

$.ajax({
                  url: "<?php echo site_url(); ?>" + "/wp-admin/admin-ajax.php",
                  type: "post",
                  data: formData,
                  processData: false,
                  contentType: false,
                  cache: false,
                  success: function(resp) {
                    const SucWra = document.querySelector(".alert");
                    SucWra.classList.add("displayblock");
                    console.log(resp);
                    window.location.href = "https://www.paypal.com/cgi-bin/webscr?&cmd=_xclick&business=............";
                    setTimeout(function(){ alert("You will be redirected to Paypal"); }, 10000);
                    $('.alert').click(function(){
                    e.stopPropagation();
                    });
                    $(document).click(function() {
                    $('.alert').hide();
                    });
                    document.getElementById("submitVisaform").innerHTML  = "Message sent!";
                  },
                  error: function(errorThrown) {
                    console.log(errorThrown);
                  }
                });

How to make validation ajax compatible of form submission + where will those error messages be displayed.

When loading your test page, the browser console shows that there’s an error in your code at line 8350 of your index page.

Hi there @Paul_Wilkins

I have changed the script a bit. I was googling and stumbled upon this.

Actually, I have made a lot of changes in the script since my first post here.

By default, there was certain kind of validations in the script(Popup validations) but initially, they were not working. I struggled for around 10 days to send email through the form, and finally, I was able to send email through Ajax and WP mail function.

But now when I was trying to validate the form by using the model given in the Github link that I have shared above – The Jquery validation(the pop validations, which were not previously working) suddenly started working in its entirety. But if you look I was only trying to validate the first name as a test, but now the whole validation is working that was already present in the original JS file.

But the Ajax is not working. So we have two possibilities now –

Number one – list of older validation work but at the same time, AJAX part should also be functional to send an email.

Number two – the older validation should stop working and Our new test validation should work along with the Ajax.

Status Currently AJAX is not working.

here is the full code β†’
https://codeshare.io/anmMbn

Thanks for the update. Your test page still seems to show that it has the same error though.

1 Like

off-topic: the type attribute for a script tag is unnecessary for Javascript; it is the default and assumed contents of any script tag.

The error on line 8350 (or in your base code, 8819) that Paul is pointing you towards is because submitHandler is not a function to be called, it is a member of the object passed to validate. its form should be submitHandler: function(form) { .....

1 Like

I have rectified the error in the original code, but still the same. Once the form is fully filled the page refreshes that means this part is executing β†’

action=""

AJAX is not being called. Previously when I was not using this part β†’ https://gist.github.com/jakebellacera/839163

AJAX was calling and script executing and sending emails.

This is my previous version of the whole code β†’ https://codeshare.io/axwwnd

https://www.diffchecker.com/d3axyAuu

Difference will be visible 8000+ onwards.

We’re going to be hard pressed to verify your javascript output when you’re showing us the raw code that is designed to be parsed through a PHP engine first.

Update your test page, and we can view source on the output.

Already done here.

apart from the code in <script></script>

One script is here also.

and Javascript is throwing an Unexpected token on line 8592.

Solve the basic syntax errors first, then ask us why it doesnt work :stuck_out_tongue:

If I try to validate the script(in shortcode.php) then the error comes β†’

I could not understand how that can be an error?

I assume Esprima is a javascript validator.
So it doesnt understand the HTML tag <script.
Leave your HTML off.

1 Like

I think syntax errors are reduced now.

I think the whole issue is that this is executing form is not submitting through AJAX.

What does your javascript console tell you when you click the submit button?

Currently it tells nothing.After submissions page refreshes, and nothing is left in console.

Then you’ve got javascript disabled. Because your code throws an error when you click the submit button.

How can the JS be disabled. because if you fill something in wrong format X or tick appears.

Not a single validations rules are written yet the validations are working from script.js β†’

It seems 2 be a priority issue. scripts.js is hijacking waht is written under:

<script></script>

Your site is telling me validate is not a function, which would indicate your site isnt loading the validation addendum to jquery.

what could be the reason? what is the solution?