PDA

View Full Version : Passing a value to a linked page, JSP content



tike0rz
Nov 27th, 2009, 09:31 PM
Hi all:

This script and code works great, but in order for me to finish, I need a way of passing to the next PHP page which check box is checked. Currently, it is just passing a single value (1, 2, 3, 4, 5, or 6) depending on the last box checked. I need a way to record which check boxes are selected.



<html>
<head>
<script type="text/javascript">
var majors = {
"001 - Exchange" : [ 2, 6 ],
"003 - Academic Foundations" : [ 2, 6 ],
"005 - Pre-Engineering" : [ 2, 6 ],
"006 - Pre-Business" : [ 2, 6 ],
"008 - Pre-Nursing" : [ 2, 6 ],
"010 - Accounting" : [ 3, 4 ],
"014 - Afro-American Studies" : [ 2, 6 ],
"050 - American Studies" : [ 2, 6 ],
"070 - Anthropology" : [ 2, 6 ],
"080 - Art" : [ 2, 6 ],
"082 - Art History" : [ 2, 6 ],
"090 - Arts and Sciences" : [ 2, 6 ],
"100 - Astronomy" : [ 2, 6 ],
"115 - Biochemistry" : [ 2, 6 ],
"120 - Biology" : [ 2, 5, 6 ],
"124 - Biomedical Technology" : [ 2, 6 ],
"130 - Botany" : [ 2, 6 ],
"135 - Business Administration" : [ 3, 4 ],
"140 - Business Law" : [ 3, 4 ],
"160 - Chemistry" : [ 2, 5, 6 ],
"163 - Childhood Studies" : [ 2, 5, 6 ],
"190 - Classics" : [ 2, 6 ],
"198 - Computer Science" : [ 2, 5, 6 ],
"200 - Creative Writing" : [ 5 ],
"202 - Criminal Justice" : [ 2, 5, 6 ],
"203 - Dance" : [ 2, 6 ],
"220 - Economics" : [ 2, 6 ],
"300 - Education" : [ 2, 6 ],
"350 - English" : [ 2, 5, 6 ],
"352 - English - American Literature" : [ 2, 5, 6 ],
"354 - English - Film Studies" : [ 2, 5, 6 ],
"360 - European Studies" : [ 2, 6 ],
"387 - Film Studies" : [ 2, 6 ],
"390 - Finance" : [ 3, 4 ],
"415 - Foreign Languages" : [ 2, 6 ],
"420 - French" : [ 2, 6 ],
"460 - Geological Sciences" : [ 2, 6 ],
"470 - German" : [ 2, 6 ],
"490 - Greek" : [ 2, 6 ],
"500 - Hebraic Studies" : [ 2, 6 ],
"509 - Historical Methods and Skills" : [ 2, 6 ],
"510 - History, General" : [ 2, 6 ],
"512 - History, American" : [ 2, 5, 6 ],
"516 - African Asian Latin American and World Hist" : [ 2, 6 ],
"520 - Home Economics" : [ 2, 6 ],
"525 - Honors Program" : [ 2, 6 ],
"533 - Human Resource Development" : [ 3 ],
"537 - Hospitality Management" : [ 3 ],
"549 - International Studies" : [ 2, 6 ],
"555 - Student Proposed Major" : [ 2, 6 ],
"560 - Italian" : [ 2, 6 ],
"565 - Japanese" : [ 2, 6 ],
"570 - Journalism" : [ 2, 6 ],
"580 - Latin" : [ 2, 6 ],
"590 - Latin American Studies" : [ 2, 6 ],
"601 - Law - Day Student" : [ 1 ],
"602 - Law - Evening Student" : [ 1 ],
"606 - Liberal Studies" : [ 2, 5, 6 ],
"615 - Linguistics" : [ 2, 5, 6 ],
"620 - Management" : [ 3, 4 ],
"623 - Management Science and Info Systems" : [ 3, 4 ],
"626 - Managerial Economics" : [ 4 ],
"630 - Marketing" : [ 3, 4 ],
"640 - Mathematics" : [ 2, 6 ],
"645 - Mathematical Science" : [ 5 ],
"660 - Medical Technology" : [ 2, 6 ],
"680 - Microbiology" : [ 2, 6 ],
"698 - Museum Studies" : [ 2, 6 ],
"700 - Music" : [ 2, 6 ],
"701 - Music, Applied" : [ 2, 6 ],
"705 - Nursing" : [ 2, 6 ],
"730 - Philosophy" : [ 2, 6 ],
"740 - Physical Education" : [ 2, 6 ],
"742 - Physical Therapy" : [ 5 ],
"750 - Physics" : [ 2, 6 ],
"760 - Physiology" : [ 2, 6 ],
"780 - Plant Physiology" : [ 2, 6 ],
"790 - Political Science" : [ 2, 6 ],
"830 - Psychology" : [ 2, 5, 6 ],
"834 - Public Administration" : [ 5 ],
"840 - Religon" : [ 2, 6 ],
"842 - Rhetoric" : [ 5 ],
"860 - Russian" : [ 2, 6 ],
"890 - General Science" : [ 2, 6 ],
"910 - Social Work" : [ 2, 6 ],
"920 - Sociology" : [ 2, 6 ],
"940 - Spanish" : [ 2, 6 ],
"950 - Speech" : [ 2, 6 ],
"960 - Statistics" : [ 2, 6 ],
"964 - Teacher Preparation" : [ 2, 6 ],
"965 - Theater Arts" : [ 2, 6 ],
"975 - Urban Studies and Community Development" : [ 2, 6 ],
"976 - Urban Planning" : [ 2, 6 ],
"981 - Volunteer Organization and Leadership" : [ 2, 6 ],
"988 - Womens & Gender Studies" : [ 2, 6 ],
"989 - Writing" : [ 2, 6 ],
"990 - Zoology" : [ 2, 6 ]
};

Array.prototype.isMember = function( find )
{
for ( var i = 0; i < this.length; ++i )
{
if ( this[i] == find ) return true;
}
return false;
}
function resetOptions( )
{
var form = document.theForm;
var sel = form.Majors;
for ( var s = sel.options.length-1; s > 0; --s )
{
sel.options[s] = null;
}

var checked = [ ];
for ( var cb = 0; cb < form.schools.length; ++cb )
{
if ( form.schools[cb].checked ) checked.push( form.schools[cb].value );
}
for( major in majors )
{
var mschools = majors[major];
for ( var c = 0; c < checked.length; ++c )
{
if ( mschools.isMember( checked[c] ) )
{
sel.options[sel.options.length] = new Option( major, major );
break;
}
}
}
}
</script>
</head>

<title>Search</title>

<body bgcolor="99CCFF">
<center>
<br>
<center>Search:<table border='1'>

<form name='theForm' action='display.php' method='post'>
<tr><td>ID: </td><td><input type="text" name="ID"/></td></tr>
<tr><td>Last Name: </td><td><input type="text" name="NAME_LAST"/></td></tr>
<tr><td>First Name: </td><td><input type="text" name="NAME_FIRST"/></td></tr>

<tr><td valign="top">School: </td>
<td>
<input type="checkbox" name="schools" value="1" onClick="resetOptions()"> Law School<br/>
<input type="checkbox" name="schools" value="2" onClick="resetOptions()"> College of Arts and Sciences<br/>
<input type="checkbox" name="schools" value="3" onClick="resetOptions()"> School of Business (Undergraduate)<br/>
<input type="checkbox" name="schools" value="4" onClick="resetOptions()"> School of Business (Graduate)<br/>
<input type="checkbox" name="schools" value="5" onClick="resetOptions()"> Graduate School<br/>
<input type="checkbox" name="schools" value="6" onClick="resetOptions()"> University College
</td>
</tr>

<tr><td>Major: </td><td><select name="Majors">
<option value="" selected>Select a major</option>
</select></td></tr>

<tr><td>Order By: </td><td>
<select name="ORDER_BY">
<option value = "" selected> Select One </option>
<option name="NAME_LAST" value = "NAME_LAST ASC"> Last Name </option>
<option name="CURRIC_CD" value = "CURRIC_CD ASC"> Major </option>
<option name="ID" value = "ID ASC"> RUID </option>
<option name="UNIT_OF_REG_CD" value = "UNIT_OF_REG_CD ASC"> School </option>
<option name="EMAIL_ADDR" value = "EMAIL_ADDR ASC"> Email Address </option>
</select></td>
</tr>
</table>

<br><br>
<input type='submit'/>

</form><br><br>
</center>
</body>
</html>


Thanks in advance.

Old Pedant
Nov 27th, 2009, 11:39 PM
No, it's not passing a single value.

You're just not reading it the right way in your JSP code.

Been 2 years since I messed with multiple checkboxes in JSP. Hang on while I refresh my head.

Oh, yeah...


String[] schools = getParameterValues( "schools" );

If you were just using getParameter() then that's why you couldn't get multiple values.

p.s.: Properly, this question should have been in the JSP forum.

Old Pedant
Nov 27th, 2009, 11:42 PM
By the by, I was wondering how you opted to solve the ID vs. name problem for the majors. I wondered if you'd use what you did:

"001 - Exchange" : [ 2, 6 ],

versus

"Exchange" : ["001", [ 2, 6 ]],
r even

"Exchange" : { "001" : [ 2, 6 ] },

tike0rz
Nov 28th, 2009, 12:27 AM
By the by, I was wondering how you opted to solve the ID vs. name problem for the majors. I wondered if you'd use what you did:

"001 - Exchange" : [ 2, 6 ],

versus

"Exchange" : ["001", [ 2, 6 ]],
r even

"Exchange" : { "001" : [ 2, 6 ] },


Hey,
I found that the value being passed was actually the label given (001 - Exchange). Since the value in the database is the number (001 in this case), I simply trimmed off all characters after the first three, and then chose to query the database with that.

Excuse me while I try out your first suggestion in the previous reply. :)

tike0rz
Nov 28th, 2009, 01:00 AM
String[] schools = getParameterValues( "schools" );



Ok, so it stores the chosen check boxes in the schools array. How do I access this in the next php page? It needs to be passed via an http POST, right?

Old Pedant
Nov 28th, 2009, 01:12 AM
Okay, now I am *utterly* confused.

Your first post's subject was--and I quote:

Passing a value to a linked page, JSP content

And now you are talking about PHP processing!

*COMPLETELY* different answers!!!!

Which is it???

tike0rz
Nov 28th, 2009, 02:03 AM
Okay, now I am *utterly* confused.

Your first post's subject was--and I quote:
And now you are talking about PHP processing!

*COMPLETELY* different answers!!!!

Which is it???

Woops. I went off on a tangent. Thanks for your help. You've done more than enough.

Old Pedant
Nov 28th, 2009, 02:43 AM
Ehhh...my fault, I guess. I missed this in your first post of this thread:


<form action='display.php' ...


I assume that means you have realized that with PHP you have to use [] at the end of your form field names, as in

<input type="checkbox" name="schools[]" value="1" onClick="resetOptions()"> Law School<br/>
???

But that means a bit of a rewrite in the JS code to handle the [] which isn't legal as part of a form field name in the DOM, as is.

Not hard. Change

for ( var cb = 0; cb < form.schools.length; ++cb )
{
if ( form.schools[cb].checked ) checked.push( form.schools[cb].value );
}

to


var schoolsCBs = form.elements["schools[]"];
for ( var cb = 0; cb < schoolsCBs.length; ++cb )
{
if ( schoolsCBs[cb].checked ) checked.push( schoolsCBs[cb].value );
}

I think that does it.

tike0rz
Nov 28th, 2009, 05:28 PM
Ehhh...my fault, I guess. I missed this in your first post of this thread:


<form action='display.php' ...


I assume that means you have realized that with PHP you have to use [] at the end of your form field names, as in

<input type="checkbox" name="schools[]" value="1" onClick="resetOptions()"> Law School<br/>
???

But that means a bit of a rewrite in the JS code to handle the [] which isn't legal as part of a form field name in the DOM, as is.

Not hard. Change

for ( var cb = 0; cb < form.schools.length; ++cb )
{
if ( form.schools[cb].checked ) checked.push( form.schools[cb].value );
}

to


var schoolsCBs = form.elements["schools[]"];
for ( var cb = 0; cb < schoolsCBs.length; ++cb )
{
if ( schoolsCBs[cb].checked ) checked.push( schoolsCBs[cb].value );
}

I think that does it.

Thanks very much. Now I just need to figure out how the value is being passed to the PHP page.

Old Pedant
Nov 28th, 2009, 08:47 PM
Which value??? The "schools[]" checkboxes??

Same as any other multiple checkbox or <select multiple>. As an array.

As I said, I'm not a PHP user, but I've seen enough PHP code to know that you do *something* like:


for ( $i = 0; $i < $_REQUEST["schools"].count(); ++$i )
{
echo "found value of " . $_REQUEST["schools"][$i] . " from the schools checkboxes<br>";
}

But check any PHP help site for the right way.

I just know that only PHP requires the silly [] in the name in the browser in order to get the multiple values in the server.

EDIT: Look here
http://us3.php.net/manual/en/faq.html.php#faq.html.arrays