1

I am a js bumbler, so this is inevitably a trivial mistake. I want a change to a select item in my form to trigger a form submit (see code snippet below).

What happens is that the form is correctly submitted to the right php page, but the Select element value defaults to the first option, regardless of the option selected. Where a I being stupid? Ron

<input type='submit' name='buttons' value='Delete'>
<input type='submit' name='buttons'  value='Copy'>
<input type='submit' name='buttons'  value='Move'>
<select name='rotate' id='rotate' onchange='this.form.submit()'>
<option value='Rotate 0'>Select from list below to Rotate</option>
<option value='Rotate 3'>Rotate by 90 CW</option>
<option value='Rotate 2'>Rotate by 180 CW</option>
<option value='Rotate 1'>Rotate by 270 CW</option>
</select>
.....other form elements
Ron
  • 11
  • 2
  • 2
    `onclick` change to `onchange` – Jai Mar 01 '17 at 10:44
  • duplicate of http://stackoverflow.com/questions/7231157/how-to-submit-form-on-change-of-dropdown-list – mplungjan Mar 01 '17 at 10:47
  • Possible duplicate of [How to submit form on change of dropdown list?](http://stackoverflow.com/questions/7231157/how-to-submit-form-on-change-of-dropdown-list) – Bugs Mar 01 '17 at 10:59
  • have changed the code to reflect 'onchange', but problem remains – Ron Mar 01 '17 at 22:20
  • Thx all. All these poss dupes show how to submit, but that is not the question - that part works. These question is that the element value (eg in PHP $_POSTS['rotate']) always contains the value of the first option (ie 'Rotate 0') even when different options are selected by the change action. Ron – Ron Mar 06 '17 at 17:11

2 Answers2

0

This is because you are using onclick which will submit the form as soon as the select is clicked. You want onchange.

thodic
  • 2,229
  • 1
  • 19
  • 35
  • Thanks. Can understand why you say that. However same problem with onchange (checked cache flushed) - in fact that is how I originally had it but tried onclick just in case. – Ron Mar 01 '17 at 11:48
0

Problem solved - I had repeated the select element at the bottom of the (long) form, so had defined the same element id twice on the page; a nono. Told you I was a bumbler. Thanks all

Ron
  • 11
  • 2
  • That can't be the cause of this problem. The Javascript doesn't use an ID for anything. – Quentin Mar 07 '17 at 09:16
  • Thx Quentin. So I guess the issue is that given that the Select Element was repeated on the page, each time with the same name, the browser posted the setting of the last iteration on the page, whereas I was was changing the first. By the way, what is the id for then? I thought it was the name of the element used by javascript. – Ron Mar 08 '17 at 14:42