views:

43

answers:

3

My HTML form is clearing automatically after I click the submit button. Any idea how to stop this from happening?

Here's the opening tag for the form:

<form onsubmit="return math()">
+2  A: 

when you click submit the page refreshes. You'll probably need to return a page with the value of the forms set to what was in when it was submitted.

alphomega
+3  A: 

One popular approach is to utilize a server-side language such as PHP which reads all the get values and recreates the HTML of the page using a template and substituting in variables.

<input type = "text" name = "Textbox1" id = "Textbox1" value = "{$_GET[Textbox1]}"/>

I've written it out as $_GET directly, but you'd really want to make sure the value was scrubbed so you don't get arbitrary html injection.

A little bit more complicated you could approach this purely client side and utilize javascript to parse the address bar and extract the values submitted to your page (assuming it wasn't submitted with POST as the method). Then you can dynamically repopulate the fields client side.

M2tM
Don't scrub, HTML-escape at the output stage. `value="<?php echo htmlspecialchars($_GET['Textbox1']); ?>"`.
bobince
The example I provided was meant to be illustrative of the simple case and so I chose to offshore the safety part of the output first. The code above could be part of a template system or anything else. Ideally you'd have a custom function "SafeHtmlValueOutput()" or something similar and you would use that in every case (avoiding the htmlspecialchars function because you may need to alter the function to remove line breaks or other interesting input that the function you chose does not support.) But good point! +1 to you sir.
M2tM
+1  A: 

If math() returns false your data won't disappear but it won't sent data to the server either. If you want to send data to the server you could make a XMLHttpRequest.

John B.