views:

35

answers:

2

Hi,

I'm learning web development, and I don't know how do this simple thing.

I've a html form with "text", "radio" and "checkbox" controls, that POST the values to the server and it saves them, for example in a file. I save the POST in raw format:

key=value&key=value&key=value&key=value

and I'd like that when the user open the form back, return the form with the saved values already in the controls.

Is there any neat way to do this? Or should I parse the POST string and set the values one by one in the controls at the server level?

I think, I could return the saved data in a hidden input control, and after with a javascript I could do a getElementById(key) and set the value to each input control... but do I need to put the post string in a special format?

How would you do it?

Thank you in advance.

+3  A: 

You should store the POST fields (preferably not in querystring style, your programming language probably has some serialization functions which are better for that) and then fill the form fields by setting value="..." attributes (or tag content e.g. for textareas).

ThiefMaster
Humm... it's easy for textboxes, but for checkboxes and radios I have to add an attribute to say which one is selected, and ensure that no other one is. For that reason I was looking for something more... straightforward to put the values back. But it seems that it's the best option... thanks!
vtortola
The cleanest way would be having a small library which generates your forms and automatically sets the default values you provide. Then the actual html behind that would be generated transparently.
ThiefMaster
+1  A: 
redben
C# .NET.That code doesn´t suit to me, but you gave me an idea about what I could do.How would you do it for checkboxes and radios?
vtortola
[Beware of XSS](http://php.net/manual/en/function.htmlspecialchars.php).
BalusC
BalusC is aright ! my code snippet is just for you to have an idea and shouldn't be used like this in any way. <br/>As for checkboxes you just need to check if the POST variable has any value...in some way. I have left the C#.net world around 2003 but, is it me or does ASP.NET provide out of the box support for data binding ? i don't think you even need to look in the POST vars ASP.net abstract that away from you, if i remember...
redben