how to send two forms with one click (script ninjaforms id)

i need the user to send 2 forms with one button-click, so i'm basically trying to merge two buttons into a third one, in order to pass data along, but don't know what i'm doing wrong here.

i've been trying to get this solution to work, but i get console-error

"uncaught typeerror: cannot read property 'submit' of null at SubmitForms"

one of the forms is generated with ninjaforms, the other one by another plugin... i'm not sure if i can address/call the ninjaform id or its name in the right way. where is any of the two to be found? i tried many different variations, all with the same result.

so, my (third) button is hooked in like this:

...
function button() {
echo 'input type="button" name=button class=button value="click me" onclick="submitForms()" /';
}

and this is what the js looks like (in header):

    script
submitForms = function(){
    document.getElementById("ninja_form_id_2").submit();
    document.getElementById("id_form_2").submit();
}
/script

i never used javascript before, so i'm not sure if i implemented it like i should. thanks for help!

Topic buttons plugins Wordpress javascript

Category Web


I partially stole this answer from SO

$("#idForm").submit(function(e) {

var url = "path/to/your/script.php"; // the script where you handle the form input.

$.ajax({
       type: "POST",
       url: url,
       data: {
                 form1: $("#idForm").serialize(),
                 form2: $("#idForm2").serialize()
       },  
                 // serializes the form's elements.
       success: function(data)
       {
           alert(data); // show response from the php script.
       }
     });

e.preventDefault(); // avoid to execute the actual submit of the form.
});

So basically you prevent the normal submit of your forms, serialize it's content to json and send that in an ajax call to the server.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.