Link to home
Create AccountLog in
JavaScript

JavaScript

--

Questions

--

Followers

Top Experts

Avatar of ddotson
ddotson

Auto Clear Form Fields after submit
I have created a form where the user types their username and password.  Upon submit, a new window pops open and the user's webmail is available to them.  The problem is that their username and password stay in the form fields in the original window. I want to clear them out after the form is submitted (for security sake).

Thanks.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of gamer_2055gamer_2055

put this in the <form> tag.
onAfterSubmit="this.reset();"

Avatar of knightEknightknightEknight🇺🇸

put this in the popup:

<BODY onload="self.opener.forms['nameOfTheOriginalForm'].reset();" >

or create a hidden field for every form field in the form, then use onChange to set the hidden field value, then write a function that will clear all the fields except hidden fields.

IMHO use gamer's solution

I was just giving another solution

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of thirdthird🇵🇭

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

hi ddotson,
you also can try somethings like this :

Test.html
==============
<Script language="Javascript">
<!--
 function SubmitNClear()
 {
    window.open (""+ loc +"","NewWindow","top=0,left=0,width=500,height=500,buttons=no,scrollbars=yes,location=no,menubar=no,resizable=no,status=no,directories=no,toolbar=no");
    document.frmSubmit.Submit();
    return true;
 }
//-->
</Script>

<Form name="frmSubmit" action="Test.html" method="Post" onSubmit="javascript:SubmitNClear()">
...All your control
<input type="submit" name="submit" value="Submit">
</Form>

Regards
x_com

gamers solution is very browser specific.
I much prefers Thirds suggestion

X_com: where does your script reset the form?

Avatar of ddotsonddotson

ASKER

gamer's solution did not work.  In fact, I searched the internet for more info in the onAfterSubmit event, and it looks like it doesn't exist.  I tried third's solution, but it did not work either.  Here is my form:

<form name="login">
      <input type="hidden" name="server" value="server/exchange">
      <font size="2"><b>Email</b></font></td>
      <b>Username:</b><input type="text" name="username" size="15" tabindex="1"><br>
      <b>Password:</b><input type="password" name="password" size="15" tabindex="1">
      <input type=button value="Login" onClick="Login(this.form)" name="button" tabindex="3">
</form>

I took out some of the formatting stuff to make it more readable.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


did you try thirds solution like so
    <input type=button value="Login" onClick="Login(this.form);setTimeout('this.form.reset()',1000);" name="button" tabindex="3">

Javascript will onyl work on a pge that's currently loaded. If your form submits to another page the onAfterSubmit() will not work. It's only possible to use it if the data is posted to another frame, pop-op window, etc.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Thanks for clarifying that Sabr.

Was that sarcasm?

No actually it wasn't.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


<!-- The answer is to refrace the question: Auto Clear Form Fields BEFORE submit -->
<script language="JavaScript" type="text/javascript">
function Login(f) {
  var Out = 'WebMail.html';                      // Name of the document you are submiting to
  Out += "?server="   + f.server.value;   // Build post-string
  Out += "&username=" + f.username.value;
  Out += "&password=" + f.password.value;
  f.username.value = '';                          // Reset form-field
  f.password.value = '';                          // Reset form-field
//f.reset();                                              // Could use f.reset() but it takes more time
  var Win = window.open('','','HEIGHT=200,WIDTH=500,scrollbars,resizable');
  Win.location = Out;     // Open submit-document if new window
  return (false);         // Return 'event handled' (avoid double posting)
}
</script>
</head>
<body>
<form name="login">
    <input type="hidden" name="server" value="server/exchange">
    <font size="2"><b>Email</b></font></td>
    <b>Username:</b><input type="text" name="username" size="15" tabindex="1"><br>
    <b>Password:</b><input type="password" name="password" size="15" tabindex="1">
    <input type=button value="Login" onClick="Login(this.form)" name="button" tabindex="3">
</form>
</body>
<!-- Dummy WebMail-document shows the submited string-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>WebMail</title></head>
<body><script>document.write('WebMail.html called by: ' + document.location.search)</script></body>
</html>

ddotson,
You need to pass the value to the paramter in new window and the data wouldnt be retain afterward. You need to add Submit function and reload the same page after pass the value to the related parameters. My first comments is emphasize about Submit() function rather than using reset funtion. Your data is SUBMIT not to RESET. However, your submitted page will be reset afterward.
About the whole idea, i'm believe you can refer terjerosenlund's code.

Regards
x_com

I believe Thirds solution <form name="form1" onsubmit="setTimeout('this.reset()',1000);">

should work just fine

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

JavaScript

JavaScript

--

Questions

--

Followers

Top Experts

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.