Skip to content

banner ajax
The Complete Reference: Ajax

Examples: One-way Iframe - POST

How do you feel about JavaScript?

Hate It - [ 1 2 3 4 5 ] - Love It



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Chapter 2 : Iframe Rating - One Way</title>
<script type="text/javascript">
 
 
function sendRequest(url, payload)
{
    
    function makeIframe()
    {
      if (window.ActiveXObject)
          var iframe = document.createElement("<iframe />");
      else
          var iframe = document.createElement("iframe");
        
      iframe.style.visibility = "hidden";
      document.body.appendChild(iframe);
      return iframe;
    }
 
    function makeIframeForm(ifr, url, payload)
    {
        var ifrDoc = null;
        var ifrWindow = ifr.contentWindow || ifr.contentDocument;
        if (ifrWindow.document)
            ifrDoc = ifrWindow.document;
        else
            ifrDoc = ifrWindow;
 
        if (!ifrDoc.body)
        {
            var html = ifrDoc.createElement("HTML");
            ifrDoc.appendChild(html);
 
            var head = ifrDoc.createElement("HEAD");
            html.appendChild(head);
 
            var body = ifrDoc.createElement("BODY");
            html.appendChild(body);
        }
 
        var ifrForm = ifrDoc.createElement("FORM");
        ifrForm.action = url;
        ifrForm.method = "POST";
        ifrDoc.body.appendChild(ifrForm);
 
 
        for (var key in payload)
        {
            var ifrText = ifrDoc.createElement("INPUT");
            ifrText.type = "text";
            ifrText.name = key;
            ifrText.value = payload[key];
            ifrForm.appendChild(ifrText);
        }
 
        return ifrForm;
    }
 
 
   var ifr = makeIframe();
   var ifrForm = makeIframeForm(ifr, url, payload);
   ifrForm.submit();
 
}
 
 
function rate(rating, comment)
{
    var ratingVal = 0;
    /* determine rating value */
    for (var i=0; i < rating.length; i++)
    {
     if (rating[i].checked)
       {
        ratingVal = rating[i].value;
        break;
       }
    }
 
    /* URL of server-side program to record rating */
    var url = "https://ajaxref.com/ch2/setrating.php";
  
    var transport = "iframe";
 
    var payload = {'rating':ratingVal, 'comment':comment, 'transport':transport}; 
 
    /* submit rating  */
    sendRequest(url, payload);
 
    /* indicate vote was made */
    var resultDiv = document.getElementById("resultDiv");
    var ratingForm = document.getElementById("ratingForm");
    ratingForm.style.display = "none";
    resultDiv.innerHTML = "Thank you for voting.  You rated this question a " + ratingVal + ".  You can see the ratings in the <a href='https://ajaxref.com/ch2/ratings.txt' target='_blank'>ratings file</a>.";
 
    resultDiv.style.display = "";
    
    /* return false to pass back to onsumbit to kill normal post */
    return false;
}
</script>
 
</head>
<body>
<h3>How do you feel about JavaScript?</h3>
<form action="https://ajaxref.com/ch2/setrating.php" method="post" id="ratingForm" onsubmit="return rate(this.rating,this.comment.value);">
<input type="hidden" name="transport" value="downgrade" />
<em>Hate It - </em> [
<input type="radio" name="rating" value="1" /> 1
<input type="radio" name="rating" value="2" /> 2
<input type="radio" name="rating" value="3" /> 3
<input type="radio" name="rating" value="4" /> 4
<input type="radio" name="rating" value="5" /> 5
] <em> - Love It</em><br /><br />
<label>Comments:<br />
<textarea id="comment" name="comment" rows="5" cols="40"></textarea></label><br />
<input type="submit" value="vote" />
</form>
 
<br />
<div style="display:none;" id="resultDiv">&nbsp;</div>
</body>
</html>