2

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.

5
  • Do you see any error logged into Chrome Dev Tools / Firebug? Commented Jun 30, 2013 at 13:26
  • I don't think you'll be able to make this work this way. submit will be the last action performed - after that the page is reloading, so you can't clear your text after submit, 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. Commented Jun 30, 2013 at 13:34
  • I just got Firebug and I don't know how to see errors. It didn't throw any at me that were immediately obvious. Commented Jun 30, 2013 at 13:37
  • @MikeW Note that the form's submit action has action="chatcontent.php" and target="chatwindow". It's submitting to an iframe, but the current page is still unchanged. I don't think it should be reloading when the submit button is pressed. Commented Jun 30, 2013 at 13:40
  • @user2536496 The page load is going on in the iframe. The old page is no longer executing after a submit. You can send the data via AJAX and then clear the text, but that will require a different architecture. Commented Jun 30, 2013 at 13:56

4 Answers 4

1

Initialize the myform by getElementById and try, like this

var myform = document.getElementById('myform');

Sign up to request clarification or add additional context in comments.

Comments

1

try

function cleartxt()
{
    myform.text.value = "";

}

<iframe width="100%" height="80%" name="chatwindow" onload="cleartxt()" src="/chatcontent.php"> 

Comments

0

Give to inputs you wish to clear some class, "clear-this" for example. Then use this script:

<script>
   var inputs = document.getElementsByClass('clear-this');
   for (var i = 0; i < inputs.length; i++ ) {
       inputs[i].value = '';
   }​​​​
</script>

Comments

0

Instead of:

function cleartxt()
{
    myform.text.value = "";
    myform.submit();
}

You could save off the value and put it into a hidden field that will submit with your form:

function cleartxt()
{
    myform.text.txtHidField.value = myform.text.value;
    myform.text.value = ""; 
    myform.submit();
}

And this goes in your form:

<input type=hidden id=txtHidField />

It's just, then, you'll have to switch out which field that chatcontent.php is expecting to get the value. Or just name the hidden field 'text' and set the textbox they are typing in to something like 'txtType', and you wouldn't have to change it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.