bandshirtguy Posted October 30, 2007 Share Posted October 30, 2007 I have a select element and I need to submit multiple values. But I also need a select all button. The code below works to select all but when I add the '[]' to the <select> line, changing it to "<SELECT NAME="subject[]" SIZE=4 MULTIPLE>. Then the select all doesn't work. But without the '[]' in the <select> tag, then it doesn't submit multiple values; just the last one selected. When I add '[] to the line I end up with this error: "document.forms[0].subject has no properties" So, how do I use a select all button but still have it pass multiple values? <head> <SCRIPT TYPE="text/javascript" LANGUAGE=JAVASCRIPT> <!-- function selectall() { for ( i=0; i<document.forms[0].subject.options.length; i++){ document.forms[0].subject.options.selected = true; } } //--> </SCRIPT> </head> <FORM method="post" action="testJScript.php"> <SELECT NAME="subject" SIZE=4 MULTIPLE> <OPTION>First</OPTION> <OPTION>Second</OPTION> <OPTION>Third</OPTION> <OPTION>Fourth</OPTION> </SELECT> <INPUT TYPE="button" ONCLICK="selectall()" VALUE="Select All"> <input TYPE="submit" NAME="submit" VALUE="Submit"> </FORM> Quote Link to comment Share on other sites More sharing options...
fenway Posted October 30, 2007 Share Posted October 30, 2007 Everything will be submitted... PHP is just dumb about it. If you must adopt PHP's silly rule, you have to use .forms[0]['subject[]'].length to get the object -- otherwise the [] will be interpreted as JavaScript code, not a string literal. Also, you can't use options.selected -- you need to select each one individually. Quote Link to comment Share on other sites More sharing options...
bandshirtguy Posted October 30, 2007 Author Share Posted October 30, 2007 For future reference.... Is there away to get around the "silly" php rule and still use PHP? I made the changes you said and it works fine now. Here is the code for future reference <head> <SCRIPT TYPE="text/javascript" LANGUAGE=JAVASCRIPT> <!-- function selectall() { for ( i=0;i<document.forms[0]['subject[]'].length;i++){ document.forms[0]['subject[]'].options.selected = true; } } //--> </SCRIPT> </head> <FORM method="post" action="testJScript.php"> <SELECT NAME="subject[]" SIZE=4 MULTIPLE> <OPTION>First</OPTION> <OPTION>Second</OPTION> <OPTION>Third</OPTION> <OPTION>Fourth</OPTION> </SELECT> <INPUT TYPE="button" ONCLICK="selectall()" VALUE="Select All"> <input TYPE="submit" NAME="submit" VALUE="Submit"> </FORM> Quote Link to comment Share on other sites More sharing options...
fenway Posted October 30, 2007 Share Posted October 30, 2007 For future reference.... Is there away to get around the "silly" php rule and still use PHP? Don't know -- I don't use PHP. This convention simply "tells" PHP that it should expect a collection and process it as such; I'm actually quite surprised it doesn't just know. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.