views:

59

answers:

4

Hello, Is it possible to auto submit a select menu when the selection changes without using a button to submit the form. I have four of these on my page and using a button for each uses up too much space. By Select Menu, I mean:

<select name="something" class="something" title="something something">
  <option selected="selected">Option One</option>
  <option >Option Two</option>
</select>

I would also like to add a confirm popup for the on change event. The following is what I use for buttons but it does not work for the select menu.

onclick="return confirm('do you haz teh codz?');"

Any link to articles tutorials or examples would be greatly appreciated!

Thanks

+2  A: 

You need a JavaScript solution, not a PHP solution. PHP can only handle form data after it's been sent to the server; i.e., when the form has been submitted.

You can use JavaScript to:

  1. Add an event listener to the <select> to trigger when it changes (onchange).
  2. When the <select> changes, get its current value.
  3. Based on the value, redirect the user (change window.location) to the page you want them to go to.

Edit: I re-read your question and if you are just looking to submit the form when the user changes the <select>, then mwotton's answer is probably the way to go. (Of course, move the JavaScript away from the HTML; it doesn't belong there.)

For your confirmation, you can do something like this:

document.getElementById('my-select').onchange = function(){
    return confirm('do you haz teh codz?');
});

(It's always a good idea to keep your JavaScript away from your HTML. So just give your <select> an ID like id="my-select" and you can access it from the JavaScript. You can also use a JavaScript library like jQuery to greatly ease your JavaScript programming.)

Josh Leitzel
josh, but how can I tell which select box was submitted and which select option was selected? thanks
Scott W.
You would have to either reference one specifically or reference a group of them (e.g., those with class name "foo"), and then check their values when they change. But if you're just looking to submit the form, I would honestly go with mwotton's solution. If you don't just want to submit the form, let me know what exactly you need help with and I'll be happy to help.
Josh Leitzel
Thank you! I have four select menus on the form each with several options. When the user selects an option I need to know which select box and select option was submitted so I can perform the appropriate action.
Scott W.
How do you want to perform the action? Do you want it to forward to your PHP script (as defined in `<form action="action.php">`), or do you want to redirect it to a different URL?
Josh Leitzel
The form submits to itself. That is why I need to know what exactly was submitted. With button submit, I know how to look for which button was clicked but with select menu I'm not sure.
Scott W.
You can see what was submitted with PHP. What I'm asking is, what do you want to happen when the user selects a new option? Do you want the form to be submitted, or do you want to send the user to a different page based on what the value of the select is?
Josh Leitzel
Oh I see. No, for each selection I have to perform a database action based on the selection but once I know how to retrieve the submitted value, I can do the rest.
Scott W.
josh, thanks for all the help. I figured it out by trial and error. I guess, retrieving select option values is no different from using the good old $_GET or $_POST. I just didn't know this. Oh well.
Scott W.
+4  A: 

This is more appropriately tagged 'javascript' since it's javascript that you'll use to do it.

Add an 'onchange' event to the select. In the example below, 'form' should be substituted for your favourite method of targeting the form node.

<select name="something" class="something" title="something something" onchange="form.submit()">
<option selected="selected">Option One</option>
  <option >Option Two</option>
</select>
mwotton
I have to agree with this one. On a change to the select menu, submit the menu's parent form...
David
mwotton, thank you! this worked well.
Scott W.
A: 

You can implement the onchange event for select object and inside the fuction make a reference to ther form.submit();

Nervo Verdezoto
A: 

Well, you need to know the form name/id or something. Assuming <form id="selectform"> and <select id="selectelement">,

window.onload=function() {
  document.getElementById('selectelement').change = function() {
    if(confirm('do you haz teh codz?'))
      document.getElementById('selectform').submit();
    else
      return false;
    return true;
  }
}

The return false/true will determine whether or not to revert the select back to the original option.

Here's a test: http://jsfiddle.net/yrqcj/2/

Kranu