admin管理员组

文章数量:1122846

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!

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!

Share Improve this question edited Dec 18, 2017 at 13:49 user133428 asked Dec 18, 2017 at 13:38 user133428user133428 14 bronze badges 2
  • first java != javascript. next you should use ajax for this or combine the 2 forms. – Thomas Commented Dec 18, 2017 at 13:45
  • that's the problem, i can't access the plugin-generated form in order to just change it. if so i would not try to find a workaround. so how would it work with ajax? i'm no dev... – user133428 Commented Dec 18, 2017 at 13:53
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: pluginshow to send two forms with one click (script ninjaforms id)