PDA

View Full Version : Need to save input from a select statement



Mareid
05-28-2007, 08:36 PM
1) Script Title:
Recall Form Values Script II

2) Script URL (on DD):
http://www.dynamicdrive.com/dynamicindex16/formremember2.htm

3) Describe problem:

I'm a volunteer for a non profit (Asperger's Association of New England), and they've asked me to make a web-based questionnaire for their users. I need to save the user's input because they are likely to fill out more than one questionaire. Users need to use a select statement to choose their state/province, and I really need to preserve it for them on future accesses to this form.

I noted that RFVII does not save the index value for a select statement, so I've been trying to add that. Unfortunately, I'm not very proficient with JavaScript, and I've struggled for 3 solid days now with nothing to show for it. Is there any chance, please please, that RFVII could be made to save (and restore) the selected index in a select statement? I'd appreciate it greatly.

Thanks in advance

Veronica
05-29-2007, 01:52 PM
You would need to set a cookie for the selected information. This script gives you some more options:

http://www.irt.org/articles/js025/

Twey
05-29-2007, 02:00 PM
Why not do it server-side?

Mareid
05-30-2007, 02:09 AM
You would need to set a cookie for the selected information. This script gives you some more options:

http://www.irt.org/articles/js025/

Veronica this is exactly what I need. Very clear explanation, I think I can do it with this as a starting point. Thank you Thank you:)

Twey
05-30-2007, 12:33 PM
It really should be saved and restored server-side. Especially since you're doing server-side processing of the data anyway, I see no reason to do it using Javascript, which is a much less accessible method.

Mareid
05-31-2007, 12:50 AM
I don't know how to do it server-side, I'm not a web programmer. I can barely cope with Javascript, as I mentioned. I have no idea whether it would be allowed on their server anyway. I'm not saving anything on the server, and the data is not processed, it's just sent to them via email.

Mareid
05-31-2007, 12:52 AM
Sigh...Veronica that was a great site for the Java script but it turns out that if your form has a method (e.g. Form Method="Post" blah blah) it doesn't work. Took me a while, by trial and error to find out that was the problem. Anyone have any other suggestions?

Veronica
05-31-2007, 01:09 AM
Other than the fact that it didn't have a way to save selected options, did the DD script you mentioned in your first post work for you? Also, how are you getting the info emailed to the people when they submit it?

Mareid
05-31-2007, 01:31 AM
I'm using a version of Formmail that I found on the web. (http://www.tectite.com/wizards/fmconf.php) and I think the version of cookie saving that I was using did save...but I'll have to go back and test again, I've been through so many iterations that I"m not positive.

Update: Yes it does work, on text fields. That's all I'm saving at the moment. If I can't figure it out, I'll just make them put in the state by typing. It's not as if it's all that hard

Veronica
05-31-2007, 02:09 AM
I'm playing around with the DD script to make it work with selected options. Check back tomorrow and I'll let you know if it works. It should, but...:rolleyes:

Mareid
05-31-2007, 11:43 AM
Thanks so much. I will check when you let me know you think you have it

Veronica
05-31-2007, 05:46 PM
OK, I tested this and it seems to work with the cookies to remember the location.

Basically, what I did is to pass the selected option into a text box with an id of "location" The, you need to add location to the persistfields in the js that's in the body.

So, inside the form add:



<script type="text/javascript">
function changeText(){
var userInput = document.getElementById('userInput').value;
document.getElementById('location').value = userInput;
}
</script>

<SELECT id="userInput" onChange="changeText()">
<OPTION id="-" VALUE="-">-</option>
<OPTION id="NY" VALUE="NY">NY</option>
<OPTION id="CA" VALUE="CA">CA</option>
</select>

<input id="location" type="text" /><br/>
<br/>


Then, in the js in the body, add location as a field to be remembered:



<script type="text/javascript">
var f1=new rememberForm("demoform") //create instance of script referencing form "demoform"
f1.persistfields("whatever", "whatever", "location") //specify the text fields to be remembered
f1.addtoggle("persistbox") //specify the checkbox "persistbox" as the " toggle box". (optional).
</script>


By passing the info and saving it to the textbox, you don't have to re-write or add to the original cookie script.

Mareid
06-01-2007, 12:45 AM
Unless I'm misunderstanding, it saves the state (e.g. DE or AZ) in that separate field, but it does not update the select box to show DE or AZ as selected. As I understand it, the only way to do that is to decode the index of the value selected...I had got as far as saving the index value correctly much as you have done for the 2-letter value, but I couldn't figure out how to restore the index
what I had was...

<select name=state onchange="this.form.elements.(some text field).value=this.selectedindex"> (I think I put it in a function, but essentially what it did was store the index to the selected value in (some text field). What I couldn't get it to do was restore the index that was saved to

form.elements.selectionfieldname[savedindex].selected=true where savedindex = the value in the text field I saved the index to.

I could not figure out how to force some event to make the recall. I was able to save the index in a cookie (just as we saved the 2-letter value) but could not seem to make the index that was saved become the selected index. The problem with the code you pointed me to first was that the body onload=(some code to get cookies and repopulate the form) worked like a charm till I added the post method to the form <form method=post blah blah> . After struggling with it for a while, I read the comments and discovered this was a well known issue with that particular code.

I'm not sure the coding shown above is exactly correct - I'm going from memory - but I did have it both saving the index to a separate field then taking the value of that field and forcing the selected index to that value, using buttons. So I would use a button to get the index and another button to write the index and both scraps of code did what I expected. The missing piece is having the index saved in a cookie (could do just like we save the 2-letter abbreviation) and having the cookie retrieved and populating the select box with the index from the saved field. It's kind of a 2-stage process, instead of just (getcookie and put it in the field(name) repeat)

Veronica
06-01-2007, 04:00 AM
You're right - the way I wrote it, it would save the state (location) to the textbox, which would then get passed to the cookie, so it could then be passed on for future visits. But it didn't save it to the combobox as the selectedIndex; the combobox just resorted back to the default dash or empty. I thought you just wanted the textbox filled in in a particular way (and that info saved), so you were using the combobox (state/province menu) to make sure they entered it that way.

Honestly, I'm not sure how to get the effect you're going for. Maybe if you post the code you have, someone else could take a look at it?

Mareid
06-02-2007, 11:37 AM
I didn't save the code, once I couldn't get the cookie part working, but it was very simple. Instead of capturing the 2-letter abbreviation, it captured the index to another field and saved it. The problem was restoring the index to the selected field from the saved index in the cookie.

I decided to just go ahead with your method and I've put the select statement back in the form, with a little read-only box next to it that saves the 2-letter state code. Not as elegant as restoring the index, but it works.

Veronica
06-03-2007, 12:34 AM
I couldn't get it to save to the cookie either, at least not with the current script, which is why I went with just passing it to the textbox. I thought of making the textbox hidden, but then when it reverted to the default, they wouldn't know if they had entered it incorrectly. So your way, making the box readonly, is a good alternative, so they can't change their state/province to some letter combo of their own choosing.

Anyway, glad it's working.