views:

1182

answers:

2

Hi,

I would like to pass information to an iframe via post. (Could be jquery or javascript that executes the post, it doesn't really matter).

The information cannot be sent via querystring as I do not have access to change the way the page brought in by the iframe is.

This data will determine the layout of the content in the iframe so how can I make it so that after the post is sent the iframe is updated? (possibly refresh?)

+1  A: 

I wrote a blog post about doing this with jQuery to upload a file using a hidden iframe. Here's the code:

Here is the HTML for the form:

<div id="uploadform">
<form id="theuploadform">
<input type="hidden" id="max" name="MAX_FILE_SIZE" value="5000000" >
<input id="userfile" name="userfile" size="50" type="file">
<input id="formsubmit" type="submit" value="Send File" >
</form>

The DIV in which to allow jQuery to create the iframe you can hide it with a little CSS:

<div id="iframe" style="width:0px height:0px visibility:none">
</div>

The DIV in which to show the results of the callback:

<div id="textarea">
</div>

The jQuery code:

<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
    $("#formsubmit").click(function() {
     var userFile = $('form#userfile').val();
     var max = $('form#max').val();
     var iframe = $( '<iframe name="postframe" id="postframe" class="hidden" src="about:none" />' );
     $('div#iframe').append( iframe );

     $('#theuploadform').attr( "action", "uploader.php" )
     $('#theuploadform').attr( "method", "post" )
     $('#theuploadform').attr( "userfile", userFile )
     $('#theuploadform').attr( "MAX_FILE_SIZE", max )
     $('#theuploadform').attr( "enctype", "multipart/form-data" )
     $('#theuploadform').attr( "encoding", "multipart/form-data" )
     $('#theuploadform').attr( "target", "postframe" )
     $('#theuploadform').submit();
     //need to get contents of the iframe
     $("#postframe").load(
      function(){
       iframeContents = $("iframe")[0].contentDocument.body.innerHTML;
       $("div#textarea").html(iframeContents); 
      } 
     );
     return false;
    });
});

</script>

I used a php app like this uploader.php to do something with the file:

<?php

$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$maxfilesize = $_POST[MAX_FILE_SIZE];

if ($maxfilesize > 5000000) {
//Halt!
   echo "Upload error:  File may be to large.<br/>";
   exit();
}else{
    // Let it go
}

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   print('File is valid, and was successfully uploaded. ');
} else {
   echo "Upload error:  File may be to large.<br/>";
}

chmod($uploadfile, 0744);
?>

There's more there than you need, but it illustrates the concept in jQuery.

jjclarkson
Wow. All of that code for something so simple? You can use the target attribute of the FORM, correlate it with the name attribute of the IFRAME, and it'll submit naturally!
Josh Stodola
@Josh, Pop that in an answer with a bit of code then ;)
Zhaph - Ben Duguid
A: 

I don't have the code handy but my team accomplished this purely in Javascript. As I recall it went something like this:

function postToPage() {
  var iframe = document.getElementById('myIFrame');

  if (iframe) {
    var newForm = '<html><head></head><body><form...> <input type="hidden" name="..." value="..." /> </form><script type=\"text/javascript\">document.forms[0].submit();</scrip' + 't></body></html>';

    iframe.document.write(newForm);  //maybe wrong, find the iframe's document and write to it
  }
}
Jeremy Bade