I have a basic web page using HTML and javascript designed to run a simple chat program. The one part of the page I can't get functioning properly is to clear the message field of the form when submitting a message. When you submit a message, the information is sent via post data to a page within an iframe on the current page, but I can't seem to get it to both submit and clear the one specific textbox. I've managed to work the code that clears the box, but submitting it messes everything up. I want to submit first, because if I clear the textbox then submit it, the message will be empty, however, there seems to be an error whenever I use the form.submit() function in javascript, and as soon as it hits that line, none of the other code will execute. I've tried returning the value of form.submit() in an alert, and it always says undefined (that is whether I put the command in the alert, or output it to a variable first). Here's my code:
<html>
<script language="javascript">
function cleartxt()
{
myform.text.value = "";
myform.submit();
}
</script>
<body bgcolor="#999999">
<form name="myform" id="myform" action="chatcontent.php" method="post" target="chatwindow" onsubmit="cleartxt();">
<table border="0">
<tr><td><font color="#FFFFFF"><b>Name:</b></font></td>
<td><input type="text" name="name" maxlength="20"></td></tr>
<tr><td><font color="#FFFFFF"><b>Color:</b></font></td>
<td><input type="text" name="color"></td></tr>
<tr><td><font color="#FFFFFF"><b>Message:</b></font></td>
<td><input type="text" name="text" size="100%" autofocus="autofocus"></td>
<td><input type="submit" name="btnsubmit" value="Submit"></td></tr>
</table>
</form>
<iframe width="100%" height="80%" name="chatwindow" src="/chatcontent.php">
</body>
This shows the submit function being used after clearing the textbox. In this form, it just clears the textbox and does nothing else. If I swapped the two lines, it would just submit the form without clearing the text.
submitwill be the last action performed - after that the page is reloading, so you can't clear your text aftersubmit, and clearing it before submits a blank message. An AJAX approach to submitting messages will probably work, if you combine it with an Ajax system to update your iframe.