Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form not clearing after user hits submit

Status
Not open for further replies.

mlm823

Programmer
Oct 29, 2003
39
US
Hello - I have a form that I've created for a site that a user enter's their email address and hits submit. Upon hitting submit it opens a new window that processes the email address and performs some functions. When you close that window and return to beginning window the information supplied is still there... I thought forms automatically clear when you hit submit. I'm not sure if this is the glitch but my submit is an image. Here is the form code

<form onSubmit="return checkNewsletter(this);" method="POST" action="subscribe.php" target="_blank">

E-Mail:
<input type="text" size="20" maxlength="150" name="emailAddr">
<img src="../images/blank.gif" width="55" height="1">
<input type="image" src="../images/SubscribeButton.jpg">
</form>

Any advice is most appreciated.

Thanks,

mlm823
 
The form would clear if you were reusing the same window and rewriting the form when you are done processing (which is the way forms are normally processed), but since you're putting the "action" page in a new window, nothing is changing in the original one (the one with the form). You have two options:
(1) Process the form in the same window (don't say "target=_blank"), and then rewrite the form when you are done. There are two easy ways to do this: (A) combine the form and processing code into one PHP file and have it call itself in the form's action (check at the top of the file to see if the POST variables exist - if so, process, else skip down to just the form); or (B) when subscribe.php is done processing, change the location back to the form file:
Code:
echo "<SCRIPT FOR=window EVENT=onload LANGUAGE=\"Javascript\">\n";
echo "window.location = \"myform.html\";\n";
echo "</SCRIPT>\n";
(2) Use Javascript to open the second window so that you have a parent/child relationship between the two, and in the child (subscribe.php) use Javascript to clear all the fields in the form on the parent.

Option #1 is easier (and of those, I personally prefer variation A over B, but I've used both), but if you really need the separate window for some reason, perhaps you have to do #2.
 
Thanks for the information. I've done option 1 several times and it is very easy to do.

Unfortunately the client wants a new window to open so that they can have more information gathered and if they don't want to enter anything that is fine..but she wanted it to open in a new window. I tried doing a clear form and giving the value entered to a hidden value to go to the new page but it didn't work either.

You mentioned setting up a parent/child relationship in javascript. I've never done anything like that before -- can you provide a suggestion on how to do that.

Thanks,

mlm823
 
Sure - here are some code snippets from my own project, modified a little for you:

In parent window:
Code:
function submit() {
    url = "subscribe.php?field1="+document.myform.field1.value+"&field2="+document.myform.field2.value;
    newwin = window.open(url,'','scrollbars=yes,WIDTH=600,HEIGHT=400');
}
In child window - this routine could be called from a "close" button or whatever:
Code:
function finish()
{
opener.document.forms['myform'].elements['field1'].value = '';
opener.document.forms['myform'].elements['field2'].value = '';
window.close();
}
My example uses two ordinary (i.e. not checkboxes or something) form fields - if you have other things, you will need to handle them accordingly to pass along their state and to restore their default state from the child.

Does that give you enough of the idea to run with it? If you have absolutely NO Javascript experience, you may need to read up on the basics a little, but opening windows is a common task - there are lots of tutorial web sites about it. The final action of filling form fields (with nothing, in your case) is also common, although I suppose doing it on another window is less common, but all you have to do is add the opener prefix to DOM element calls that would otherwise operate in the same window. There are various ways to reference DOM elements - the way I showed is one that is fairly browser independent, at least working in IE and Firefox.

Everyone, sorry that I'm giving Javascript code examples on the PHP web site, but it seemed kinder than sending mlm823 away to the JS forum, since I happened to know what to do. [orientalbow]
 
I as well want to apologize for the javascript language appearing in the php forum.

Thank you for the information. I tweaked it to work for the data and code I have.

Mlm823
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top