简体   繁体   中英

Submit Form with ajax result

I successfully load iframe into div #output with AJAX. This script play video for me.

jQuery( document ).ready( function( $ ) {
    $( '.play_next' ).on('click', function( event ) {
        event.preventDefault();
        $('#output').empty();
        var meta_key = $( this ).data( 'meta-key' );
        var post_id = $( this ).data( 'post-id' );
        $.ajax( {
            url: ajaxobject.ajaxurl,
            type: 'POST',
            dataType: 'html',
            data: {
                action : 'wpse_296903_call_meta',
                post_id : post_id,
                meta_key : meta_key,
            },
            success: function( result) {
                $( '#output' ).append( result );
            }
        });
    });
});

Now no more need of #output in div, Need to submit this value with form.

<form action="http://other-domain.com/" method="POST" target="_black">
    <input type="submit" name="iframe" value="" />
</form>

This form submit iframe to other domain, which open in new tab with property target="_black .

How put #output value in Form and submit with Ajax?

Edit

You can not access the contents of an iframe from outside of one if its an iframe with a different origin.

If its the same origin

 let form = document.createElement('form');
 let input = document.createElement('input');
 input.type = 'hidden';
 input.value = encodeURIComponent(document.querySelector('iframe').contentDocument.innerHTML);
 input.name = 'iframe_data';
 form.action = 'myDestination.html'
 form.appendChild(input);
 document.body.appendChild(form);
form.submit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM