1

Possible Duplicate:
How do you post to an iframe?

I have a form that I'd like to have do two things - submit a standard form request with one button (which works), and have a second button (that also works, partially) that opens an iframe inside the page with the value from the form appended to the iframe URL.

Here's the script I'm using:

<script language="JavaScript" type="text/javascript"> 
function makeFrame() { 
ifrm = document.createElement("IFRAME"); 
ifrm.setAttribute("src", "https://www.hostname.com/filepath"); 
ifrm.style.width = 340+"px"; 
ifrm.style.height = 630+"px"; 
document.body.appendChild(ifrm); 
} 
</script>

and the form:

<FORM action="https://www.hostname.com/filepath" method="POST" name="form" id="viewGift">
  <P><br>
    <LABEL for="giftId">giftId: </LABEL><INPUT type="text" name="giftId"><BR>
    <INPUT type="radio" name="opt1" value="1" checked> opt1<BR>
    <INPUT type="radio" name="opt2" value="2"> opt2<BR>
    <INPUT type="radio" name="opt3" value="3"> opt3<BR>
    <INPUT type="radio" name="opt4" value="4"> opt4<BR>
    <INPUT type="button" value="Send Gift" onclick="formSubmit()">
    <INPUT type="button" value="Show Gift" onclick="makeFrame()">
 </P>
 </FORM>

My form submits correctly, but I can't figure out how to get it to pass the form giftId value to the iframe URL with the Show Gift button. If I hardcode the URL, the Show Gift button works just fine, but that doesn't help much for the page's purpose.

Basically, I need the Show Gift button to create an iframe with the URL of https://www.hostname.com/filepath/giftId , where giftId is entered by the user.

Community
  • 1
  • 1

1 Answers1

0

This should work:

function makeFrame() {
    var ifrm = document.createElement("IFRAME");
    var gift_id=document.getElementById('viewGift').elements['giftId'].value
    ifrm.setAttribute("src", "https://www.hostname.com/filepath/"+gift_id);
    ifrm.style.width = 340 + "px";
    ifrm.style.height = 630 + "px";
    document.body.appendChild(ifrm);
}
charlietfl
  • 164,229
  • 13
  • 110
  • 143
  • Thanks! I had to tweak it a smidge, Chrome wanted the final } to be on the same line as the document.body line. Now I just gotta figure out how to embed the iframe into a table instead of appending it to the end of the body text :-) Off to Google! – Les Thompson Jan 28 '13 at 01:47