1

TL;DR

How to make a form to make a normal post instead of making a submission via JQuery?


I bought a theme recently and I'm making a form with several steps. I'm using the "Wizard" component of this theme and you can see how it works through this link:

https://keenthemes.com/metronic/preview/demo1/custom/pages/wizard/wizard-2.html

My problem is with the submission of this form. I would like to make a common submission. As it stands, the form must be submitted using JQuery.

I made some changes to my form to be able to make a common submission. For example:

I informed the POST method and an action for the form:

<form class="m-form m-form--label-align-right- m-form--state-" id="m_form" action="sale/new" method="post" enctype="multipart/form-data">

And I also created a "Submit" button:

<button type="submit" class="btn btn-primary" data-wizard-action="submit">Save</button>

For the Wizard work, the form must have an id (m_form) and the submit button must also have the attribute data-wizard-action="submit".

I am using the javascript of the theme itself, and it is precisely because I don't know much about JS that I believe I am facing problems. I tried to remove the e.preventDefault, but the form still does not POST to the action I determined. The javascript code for the theme I'm using is this:

//== Class definition
var WizardDemo = function () {
    //== Base elements
    var wizardEl = $('#m_wizard');
    var formEl = $('#m_form');
    var validator;
    var wizard;

    //== Private functions
    var initWizard = function () {
        //== Initialize form wizard
        wizard = new mWizard('m_wizard', {
            startStep: 1
        });

        //== Validation before going to next page
        wizard.on('beforeNext', function(wizardObj) {
            if (validator.form() !== true) {
                wizardObj.stop();  // don't go to the next step
            }
        })

        //== Change event
        wizard.on('change', function(wizard) {
            mUtil.scrollTop();            
        });

        //== Change event
        wizard.on('change', function(wizard) {
            if (wizard.getStep() === 1) {
                // alert(1);
            }           
        });
    }

    var initValidation = function() {
        validator = formEl.validate({
            //== Validate only visible fields
            ignore: ":hidden",

            //== Validation rules
            rules: {
                //=== Client Information(step 1)
                //== Client details
                name: {
                    required: true 
                },
                email: {
                    required: true,
                    email: true 
                },       
                phone: {
                    required: true,
                    phoneUS: true 
                },     

                //== Mailing address
                address1: {
                    required: true 
                },
                city: {
                    required: true 
                },
                state: {
                    required: true 
                },
                city: {
                    required: true 
                },
                country: {
                    required: true 
                },

                //=== Client Information(step 2)
                //== Account Details
                account_url: {
                    required: true,
                    url: true
                },
                account_username: {
                    required: true,
                    minlength: 4
                },
                account_password: {
                    required: true,
                    minlength: 6
                },                

                //== Client Settings
                account_group: {
                     required: true
                },                
                'account_communication[]': {
                    required: true
                },

                //=== Client Information(step 3)
                //== Billing Information
                billing_card_name: {
                    required: true
                },
                billing_card_number: {
                    required: true,
                    creditcard: true
                },
                billing_card_exp_month: {
                    required: true
                },
                billing_card_exp_year: {
                    required: true
                },
                billing_card_cvv: {
                    required: true,
                    minlength: 2,
                    maxlength: 3
                },

                //== Billing Address
                billing_address_1: {
                    required: true
                },
                billing_address_2: {

                },
                billing_city: {
                    required: true
                },
                billing_state: {
                    required: true
                },
                billing_zip: {
                    required: true,
                    number: true
                },
                billing_delivery: {
                    required: true
                },

                //=== Confirmation(step 4)
                accept: {
                    required: true
                }
            },

            //== Validation messages
            messages: {
                'account_communication[]': {
                    required: 'You must select at least one communication option'
                },
                accept: {
                    required: "You must accept the Terms and Conditions agreement!"
                } 
            },

            //== Display error  
            invalidHandler: function(event, validator) {     
                mUtil.scrollTop();

                swal({
                    "title": "", 
                    "text": "There are some errors in your submission. Please correct them.", 
                    "type": "error",
                    "confirmButtonClass": "btn btn-secondary m-btn m-btn--wide"
                });
            },

            //== Submit valid form
            submitHandler: function (form) {

            }
        });   
    }

    var initSubmit = function() {
        var btn = formEl.find('[data-wizard-action="submit"]');

        btn.on('click', function(e) {
            e.preventDefault();

            if (validator.form()) {
                //== See: src\js\framework\base\app.js
                mApp.progress(btn);
                //mApp.block(formEl); 

                //== See: http://malsup.com/jquery/form/#ajaxSubmit
                formEl.ajaxSubmit({
                    success: function() {
                        mApp.unprogress(btn);
                        //mApp.unblock(formEl);

                        swal({
                            "title": "", 
                            "text": "The application has been successfully submitted!", 
                            "type": "success",
                            "confirmButtonClass": "btn btn-secondary m-btn m-btn--wide"
                        });
                    }
                });
            }
        });
    }

    return {
        // public functions
        init: function() {
            wizardEl = $('#m_wizard');
            formEl = $('#m_form');

            initWizard(); 
            initValidation();
            initSubmit();
        }
    };
}();

jQuery(document).ready(function() {    
    WizardDemo.init();
});

I would like to know if there is any way to keep the Wizard and the validation working, but in a way that it is possible to make a common submission for the action that I defined for the form.

  • I got lost a little bit. So you want to do a submission like the browser does using plain HTML
    element, but you want the validation to happen through jQuery, am i right ?
    – Eyad Mohammed Osama Apr 08 '20 at 01:54
  • Looks like your action set to `sale/new` do you have an index.php page in that directory that checks the post values? – dale landry Apr 08 '20 at 02:01
  • Do you want to parse the post info on the same page as the form or at that directory? /-> `current_directory_form_is_at/sale/new/`, and if you want to go to that directory `sale/new` what is the name of the file that will be parsing the `$_POST` code? – dale landry Apr 08 '20 at 02:03
  • Exact, @EyadMohammedOsama Almost that, in fact. It is not just about validation. It is also about the form working as a wizard, as I show in the first link of my question. Everything is working fine, the question is just the post that I would like to treat as if it were a common submission. – Carlos Augusto Apr 08 '20 at 02:18
  • Actually I'm using the Slim Framework to track routes, @dalelandry – Carlos Augusto Apr 08 '20 at 02:19
  • Can you try using class ```var formEl = $('.m-form');``` instead. or use ```var formEl = $(this).closest("form")``` and do all the validation inside button click event (function) using ```this``` keyword. Let me know if you want code. – Arjun Singh Apr 08 '20 at 03:18
  • I tried to treat it as a class as you suggested @ArjunSingh , but the Wizard stopped working = / – Carlos Augusto Apr 08 '20 at 03:40
  • Go through this link https://codeshare.io/5NVL64 – Arjun Singh Apr 08 '20 at 04:45

1 Answers1

0

You just need a slight change in your initSubmit() function.

var initSubmit = function() {
    var btn = formEl.find('[data-wizard-action="submit"]');

    btn.on('click', function(e) {
        e.preventDefault();

        if (validator.form()) {
            //== See: src\js\framework\base\app.js
            mApp.progress(btn);
            //mApp.block(formEl); 

            //== See: http://malsup.com/jquery/form/#ajaxSubmit
            /*
            formEl.ajaxSubmit({
                success: function() {
                    mApp.unprogress(btn);
                    //mApp.unblock(formEl);

                    swal({
                        "title": "", 
                        "text": "The application has been successfully submitted!", 
                        "type": "success",
                        "confirmButtonClass": "btn btn-secondary m-btn m-btn--wide"
                    });
                }
            });
            */

            // Use this one instead:
            formEl.submit();
        }
    });
}

This simply means that: Instead of doing an AJAX request if validation result is positive, just do a normal submission.

Here's the reference for the submit() method of jQuery: https://api.jquery.com/submit/

By the way, this question can be found under the title (how to submit form using jQuery ?), see this question for example: Submit a form using jQuery

  • I tried to do this, the post is not made for the action I defined in the form. The final page remains as it is and an animation appears on the "Save" button (spinner loading) that indicates that something is happening, but the page remains as it is. My form's action is not being called, it seems. – Carlos Augusto Apr 08 '20 at 02:38