javascriptjqueryajaxformsplone-4.x

Submitting form without opening new window or tab


So I'm trying to submit a form using Ajax in order to prevent it from opening a new tab upon submition (Which gets very annoying when creating several images, since a new tab would be generated with every image)

This will submit the form, and function properly, however it will open a new tab.

// Submits form
$("#image-base-edit").submit();

However I tried @abc123 suggestion on this post and I modified his code to look like the snippet below, however by submitting it this way, the image isn't actaully created.

/* attach a submit handler to the form */
$("#image-base-edit").submit(function(event) {

    /* stop form from submitting normally */
    event.preventDefault();

    /* get some values from elements on the page: */
    var $form = $(this),
        term1 = $form.find('input[name="id"]').val(),
        term2 = $form.find('input[name="title"]').val(),
        term3 = $form.find('input[name="image_file"]').val(),
        url = $form.attr('action');

    /* Send the data using post */
    var posting = $.post(url, {
        name: term1,
        title: term2,
        image_file: term3
    });

    /* Prints Done */
    posting.done(function(data) {
        console.log("done");
    });
});

// Submits form
$("#image-base-edit").submit();

Here is the HTML form:

<!--Image Create FORM-->
<form name="edit_form" method="post" enctype="multipart/form-data" class="enableUnloadProtection enableAutoFocus enableFormTabbing enableUnlockProtection d-none" action="" id="image-base-edit" target="_blank">
    <fieldset id="fieldset-default" class="formPanel" style="display: block;">
        <legend id="fieldsetlegend-default" style="visibility: hidden; font-size: 0px; padding: 0px; height: 0px; width: 0px; line-height: 0;">Default</legend>
        <input name="id" value="image.2018-05-10.9629264509" originalvalue="image.2018-05-10.9629264509" type="hidden">
        <div class="field ArchetypesStringWidget  kssattr-atfieldname-title" data-fieldname="title" data-uid="0fd3d162687e4bd8917bc9830d616043" id="archetypes-fieldname-title">
            <input name="title" class="blurrable firstToFocus" id="title" value="" size="30" maxlength="255" placeholder="" type="text">
        </div>
        <div class="field ArchetypesImageWidget  kssattr-atfieldname-image" data-fieldname="image" data-uid="0fd3d162687e4bd8917bc9830d616043" id="archetypes-fieldname-image">
            <div id="appendInputhere">
            </div>
        </div>
    </fieldset>
    <div class="formControls">
        <input name="form.submitted" value="1" originalvalue="1" type="hidden">
        <input class="context" name="form.button.save" value="Save" type="submit">
    </div>
</form>

And the content that gets appened into the "appendInputhere" looks like this.

<input size="30" name="image_file" id="image_file" type="file">

If there is an easier way to do this, that I may be overlooking please let me know. Basically I want the form to function the exact same way it currently does, other than opening a new tab, or loading a new page in the users window, I want the user to not have to see a change.


Solution

  • So while reading around more, and trying to find a way to do the original method I wrote about above, I learned that it is possible to set the target of a form to an iframe. So to solve my issue above, created this iframe...

    <iframe name="formSubmitFrame" name="formSubmitFrame" tile="Holds Submitted form data" rel="nofollow" class="d-none"></iframe>
    

    and had it set to not display anywhere. Then, I set the target of my iframe to "formSubmitFrame".

    This ultimately achieved my goal of submitting a form without the user seeing any window or tab changes.