Forum Moderators: open

Message Too Old, No Replies

Using a select option to automatically submit a form

         

wfernley

5:34 pm on Aug 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello. I was curious how to set a select box so that when the user clicks an option it automatically submits the form.

I have designed my site in PHP and I have a select box on the top of my page which lets the user select their country. I want to make it so when they select their country it assigns a session variable with the country as the value.

What would be the best way to do this?

Thanks :)

Wes

BlobFisk

6:47 pm on Aug 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi wfernley,

You can do this using the onchange event handler. On the <select> you add an onchange event that submits the form:

<select onchange="this.form.submit()">

I would recommend against this from a usability point of view. It means that if the user selects an incorrect option (to the one they really wanted to choose) that the form gets submitted and they may be taken to another page.

If you are using this as a menu system, a small submit button with a value of "Go" would increase your sites usability no end.

If it is part of a form I would highly recommend against it (again from a usability standpoint) as it does not allow the user to change any of their inputs before the <select> or enter any data after the <select>.

HTH

wfernley

8:09 pm on Aug 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Blobfisk! :)

Yeah I can understand where that function would cause problems. But for what I will be doing it is perfect. For example if you go to [belkin.com...] and at the top right you see a section to choose your country. That is what I need to do. I have a small section to add it so a GO button wouldn't fit. In other sections that contain SELECT boxes I have added a go button because like you said they wouldn't be able to add any more information after that box and also they may click the wrong value.

Thanks again :)

wfernley

6:27 pm on Aug 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi.

After fooling around with this.form.submit() I can't seem to get this working. Is there a Javascript needed in the HEAD tag? If so what should I put?

A tutorial and doing this kind of function would be great :)

Thanks.

wfernley

1:06 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Me again ;)

I found this tutorial which helped me to get it working. I know nothing about Javascript but the script seemed to work great!

Here is the link: [quirksmode.org...]

Thanks again for your help ;)