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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.