Target an iframe with a HTML Post with jQuery Target an iframe with a HTML Post with jQuery ajax ajax

Target an iframe with a HTML Post with jQuery


You can do this via a regular form:

<form action="targetpage.php" method="post" target="iframename" id="formid">   <input type="hidden" name="foo" value="bar" /></form>

You can then use jQuery to submit the form:

$("#formid").submit();


Maybe you are missing the point of an AJAX request - why are you trying to specify the "target" of an asynchronous request? This makes no sense as the whole point of AJAX is that the request from the server gets sent back to the Javascript, free of page reloads or HTML destinations.

If you wanted to load the result of the request onto an iframe, you might do:

$.post('myurl', function(data) {    $('#myframe').html(data);}, 'html');


You can solve the no-form-allowed-within-a-form problem by dynamically creating the form and appending it to the body. So you'd do something like this:

$().ready(function() {    $('body').append('<form        action="url"        method="post"        target="iframename"        id="myspecialform">         <input type="hidden" name="parameter" value="value" />       </form>');});

That creates your iframe-updating form outside of the main form that encompasses everything else on the page. Then just call it as Josh recommended: $('#myspecialform').submit();.