Jump to content

[SOLVED] form select all and submit multipule values problem


bandshirtguy

Recommended Posts

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>

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.

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>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.