0

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!

| improve this question | | | | |
  • first java != javascript. next you should use ajax for this or combine the 2 forms. – Thomas Dec 18 '17 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 Dec 18 '17 at 13:53
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.

| improve this answer | | | | |
  • well, thank you. unfortunately i have no clue what to do with this. i have no "script to handle the input" and as i said i don't know where to get the ninjaforms-id from... can you clarify that for a complete beginner? i'd appreciate it! thanks. – user133428 Dec 18 '17 at 16:06
  • I'm affraid you cannot do it without some programming knowledge. I don't have to write the entire code for you at the moment. You might want to start with learning how to write jQuery ajax requests. – Thomas Dec 18 '17 at 16:10
  • isn't this why this website exists? to learn? – user133428 Dec 18 '17 at 16:12
  • Yes, however in this field it is very hard to just understand part 25 without first making an attemp at part 1 to 24. This answer is in my opinion pretty good and not that hard for your problem, it just requires some effort from your part to learn the basics of jQuery. – Thomas Dec 18 '17 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.