PricingBlog

Manually trigger form submit?

  • neville9288-1283573598249947259

    Neville

    1 year ago

    Hey folks! Anyone know if it's possible to manually trigger a form submit? I tried a bit of Javascript that gets called when a button is clicked:

    function submitForm({ formID }, ctx) {
    document.getElementById(formID).submit();
    }

    but that does not seem to actually submit the form (in the form Submit event, I log to console but that never appears...)

    Any ideas?
  • chrislaupama-1283594146057031711

    Chris Laupama

    1 year ago

    I don't think I've ever done it this way, but maybe this? - maybe explain the use case?
    1283594146162147358-toddle_2024-09-12_at_1.00.30_PM.jpg
  • lucasg-1283606634395861044

    Lucas G

    1 year ago

    It would be
  • function testformsubmit (args, ctx) { document.getElementById(args.formID).submit();
    }
  • Though I don't know why you would want to do that vs in the UI as Chris showed
  • neville9288-1283738894147522660

    Neville

    1 year ago

    Thanks 🙂

    Here's the use case: I'm building a document management system. A document has metadata (6-7 fields), and up to 4 types of content (Word and PDF versions, and an HTML version I generate from Word plus any images from the original Word source). Updating the metadata works fine with a standard form submit.

    The document content (Word, PDF etc) can get quite large, so I want to upload them individually. To that end, I built a file-selector-and-uploader component. This component has a number of attributes including content type (Word, PDF etc) to determine where the content should go. Inside the component, there is a file input field wrapped in a form, as well as 4 x API calls, one for uploading each type of content. Selecting the file (single or multiple) inside the component works fine.

    In the UI, I present the user with 4 of these components, one for each type of content. The user has to select at least an HTML or PDF version and optionally Word and images.

    When they've selected the content they want to upload, a "master upload" button changes the "state" attribute of each component into "load". The component catches the attribute state change, and then submits the form (<-- this is the bit I want to get working). Submitting the form will call the appropriate API with the body encoded as multipart/form-data.

    I'm doing it this way because my experiments show that API calls (to Xano in this case) are only correctly encoded if you submit the form (or in my case, 4 forms each inside a component).

    I know there are other ways of doing this. For example, I think I could call the API manually without submitting the form, but then I would need to base64 encode the files so I can use an application/json content type.

    Hope that helps explain why I need to manually trigger the form submit (to correctly encode the multipart/form-data).

    I could of course be going about this all wrong - I'm fairly new to Toddle!
  • neville9288-1283750579994361917

    Neville

    1 year ago

    UPDATE: The form actually does get submitted - a test project shows the screen go blank and then reload. But the Toddle code attached to the form submit does not get executed... 😦
  • lucasg-1283751856551493744

    Lucas G

    1 year ago

    There's a few different ways that you can go about this
  • lucasg-1283752180494368850

    Lucas G

    1 year ago

    First, though, I'm pretty sure you'd be able to have just one component and handle the logic of which API to call based on file type. The component can still take multiple files then a workflow can run for each file and upload them accordingly
  • lucasg-1283752625069625397

    Lucas G

    1 year ago

    The reason why it wouldn't execute though is because the snippet you're using doesn't have prevent default
  • Browsers always refresh the page when a form submits which is why you almost always want to add prevent default to them
  • You can add it either in toddle on the submit event or in the function itself
  • neville9288-1283756784472821913

    Neville

    1 year ago

    thanks Lucas. When my custom action looks like this:

    function submitForm(args, ctx) {
    document.getElementById(args.formID).preventDefault();
    document.getElementById(args.formID).submit();
    }

    I get this:
    document.getElementById(...).preventDefault is not a function

    I've had this working before as you suggested: using the file picker as a file picker only, and wrapping all 4 components in a form which uses the standard form submit.

    This resulted in a whole bunch of variabes on the page (to store each file/files, track the status of each upload etc), so I thought it would be really neat if I could make each file picker also do the upload.

    If I can get that preventDefault working I think I will be there. I have it in the action (which throws an error) as well as the form submit event - but as I said the code associated with the Toddle submit event does not seem to get executed.
  • neville9288-1283757424498704424

    Neville

    1 year ago

    (suspect I may need to manually add an event listener to the form to prevent the default, but that sort of defeats the object of using Toddle...)

    I will see if there's a better way to engineer this.
  • lucasg-1283759723438538763

    Lucas G

    1 year ago

    Yeah you’d need an event listener on the submit event