AJAX - The Complete Reference

(avery) #1

28 Part I: Core Ideas


break;
}
}

/* URL of server-side program to record rating */
var url = "http://ajaxref.com/ch2/setrating.php";
var transport = "iframe";
var payload = {"rating":ratingVal, "comment":encodeValue(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='http://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="http://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"> </div>
</body>
</html>

NNOT EOTE If you try to show the values of the iframe form yourself by making the frame visible, you
may not see anything, depending on the browser in question and how you pause the submission.
To provide screen-visible debugging of hidden posted iframes, you may be forced to return to tried
and true methods like document.write() instead of the DOM, even in the most modern
browser. An example of this can be found at http://ajaxref.com/ch2/onewayiframepostvisible.html.
Be careful as you may see exceptions in browsers when running this code.
Free download pdf