nested arrays and custom objects in javascript

  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

ok, im working on this project for school and well, i have no idea what the problem is... its coming up with an 'object expected' error... any suggestions?

Code: [ Select ]
<html>
    <head>
        <title>Find Student</title>
        <script type="text/javascript"></script>
        <script>
            var year = new Array();
                var year[0] = new gradYear();
                    var year[0].gradYear = 2005;
                    var year[0].student = new Array();
                        var year[0].student[0] = new ident();
                        var year[0].student[0].name = "Schrodinger, Cae";
                        var year[0].student[0].userName = "schrodingerc";
            function gradYear(year, student){
                this.year = year;
                this.student = student;
            }
            function ident(name, userName){
                this.userName = userName;
                this.name = name;
            }
            function populate(){
                alert(year.length);
            }
            /*
            function refreshStudentList(){
                var selectedYearIndex = ____ ;
                for(var i = 0; i <= (year[selectedYearIndex].student.length() - 1); i++){
                    studentMenu.options[i] = new Option(year[selectedYearIndex].student[i].name, year[selectedYearIndex].student[i].userName);
                }
            }
            function redirect(){
                var targetURL = '/default.asp?student?' + document.findStudent.studentMenu.value;
              window.location.href = targetURL;
            }
            */
        </script>
    </head>
    <body onload = "populate();">
        <center>
            <table height ="250" width = "500">
                    <tr height = "15" valign = "bottom">
                        <td width = "105">
                            Graduation Year
                        </td>
                        <td>
                            Student Name
                        </td>
                    </tr>
                    <tr valign = "top">
                        <form name = "findStudent" action="" method="POST">
                            <td height="235">
                                <select name = "yearMenu" size = "10" width = "150" style = "width: 100; height:200" onchange = "refreshStudentList();" onclick="refreshStudentList();">    
                                    <option>----------------</option>    
                                </select>
                            </td>
                            <td>
                                <select name = "studentMenu" size = "10" width = "150" style = "width: 300; height:200">                                                                                                     
                                    <option>----------------</option>
                                </select>
                            </td>
                            <td>
                                <input type = "submit" value = "Submit" onclick = "rediredt()">
                            </td>
                        </form>
                    </tr>
            </table>
        </center>
    </body>
</html>
  1. <html>
  2.     <head>
  3.         <title>Find Student</title>
  4.         <script type="text/javascript"></script>
  5.         <script>
  6.             var year = new Array();
  7.                 var year[0] = new gradYear();
  8.                     var year[0].gradYear = 2005;
  9.                     var year[0].student = new Array();
  10.                         var year[0].student[0] = new ident();
  11.                         var year[0].student[0].name = "Schrodinger, Cae";
  12.                         var year[0].student[0].userName = "schrodingerc";
  13.             function gradYear(year, student){
  14.                 this.year = year;
  15.                 this.student = student;
  16.             }
  17.             function ident(name, userName){
  18.                 this.userName = userName;
  19.                 this.name = name;
  20.             }
  21.             function populate(){
  22.                 alert(year.length);
  23.             }
  24.             /*
  25.             function refreshStudentList(){
  26.                 var selectedYearIndex = ____ ;
  27.                 for(var i = 0; i <= (year[selectedYearIndex].student.length() - 1); i++){
  28.                     studentMenu.options[i] = new Option(year[selectedYearIndex].student[i].name, year[selectedYearIndex].student[i].userName);
  29.                 }
  30.             }
  31.             function redirect(){
  32.                 var targetURL = '/default.asp?student?' + document.findStudent.studentMenu.value;
  33.               window.location.href = targetURL;
  34.             }
  35.             */
  36.         </script>
  37.     </head>
  38.     <body onload = "populate();">
  39.         <center>
  40.             <table height ="250" width = "500">
  41.                     <tr height = "15" valign = "bottom">
  42.                         <td width = "105">
  43.                             Graduation Year
  44.                         </td>
  45.                         <td>
  46.                             Student Name
  47.                         </td>
  48.                     </tr>
  49.                     <tr valign = "top">
  50.                         <form name = "findStudent" action="" method="POST">
  51.                             <td height="235">
  52.                                 <select name = "yearMenu" size = "10" width = "150" style = "width: 100; height:200" onchange = "refreshStudentList();" onclick="refreshStudentList();">    
  53.                                     <option>----------------</option>    
  54.                                 </select>
  55.                             </td>
  56.                             <td>
  57.                                 <select name = "studentMenu" size = "10" width = "150" style = "width: 300; height:200">                                                                                                     
  58.                                     <option>----------------</option>
  59.                                 </select>
  60.                             </td>
  61.                             <td>
  62.                                 <input type = "submit" value = "Submit" onclick = "rediredt()">
  63.                             </td>
  64.                         </form>
  65.                     </tr>
  66.             </table>
  67.         </center>
  68.     </body>
  69. </html>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Code: [ Select ]
     function gradYear(year, student){
      this.year = year;
      this.student = student;
     }
  1.      function gradYear(year, student){
  2.       this.year = year;
  3.       this.student = student;
  4.      }

what does this refer to? I only glanced and my js isn't great anymore, but that looks decidedly suspicious?
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Don't use var over and over again for the same variable. You only need to declare year once. After that you're just accessing an existing array of objects.

Code: [ Select ]
    var year = new Array();
    year[0] = new gradYear();
    year[0].gradYear = 2005;
    year[0].student = new Array();
    year[0].student[0] = new ident();
    year[0].student[0].name = "Schrodinger, Cae";
    year[0].student[0].userName = "schrodingerc";
  1.     var year = new Array();
  2.     year[0] = new gradYear();
  3.     year[0].gradYear = 2005;
  4.     year[0].student = new Array();
  5.     year[0].student[0] = new ident();
  6.     year[0].student[0].name = "Schrodinger, Cae";
  7.     year[0].student[0].userName = "schrodingerc";
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

ah hell... that was it... :banghead:

Quote:
function gradYear(year, student){
this.year = year;
this.student = student;
}


its a user defined object (i think thats what its called) its more-or-less the equivelent of a class in standard java...
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

same project, my menus are working now, however my redirect is not working... it looks fine to me, though i may be missing something... when you click the button it should redirect you yto 'default.asp?student?*value_selected_student*' and i know that it is generating the URL correctly because i have placed alerts in it and its fine... i have also tried it with the whole URL http://server/default.asp... no go... any suggestions?

Code: [ Select ]
<html>
    <head>
        <title>Find Student</title>
        <script type="text/javascript"></script>
        <script>
            var year = new Array();
                year[0] = new gradYear();
                    year[0].gradYear = "2005";
                    year[0].student = new Array();
                        year[0].student[0] = new ident();
                        year[0].student[0].name = "Schrodinger, Cae";
                        year[0].student[0].userName = "schrodingerc";
                        year[0].student[1] = new ident();
                        year[0].student[1].name = "Name, No";
                        year[0].student[1].userName = "namen";
                year[1] = new gradYear();
                    year[1].gradYear = "2006";
                    year[1].student = new Array();
                        year[1].student[0] = new ident();
                        year[1].student[0].name = "Wiggen, Ender";
                        year[1].student[0].userName = "wiggene";
                year[2] = new gradYear();
                    year[2].gradYear = "2007";
                    year[2].student = new Array();
                        year[2].student[0] = new ident();
                        year[2].student[0].name = "Clue, No";
                        year[2].student[0].userName = "cluen";
            function gradYear(gradYear, student){
                this.gradYear = gradYear;
                this.student = student;
            }
            function ident(name, userName){
                this.name = name;
                this.userName = userName;
            }
            function populate(){
                document.findStudent.gradYearMenu.options.length = 0;
                for(var i = 0; i <= (year.length - 1); i++){
                    document.findStudent.gradYearMenu.options[i] = new Option(year[i].gradYear, i);
                }
            }
            
            function repopulateStudents(){
                document.findStudent.studentMenu.options.length = 0;
                for(var i = 0; i <= (year[document.findStudent.gradYearMenu.value].student.length - 1); i++){
                    document.findStudent.studentMenu.options[i] = new Option(year[document.findStudent.gradYearMenu.value].student[i].name, year[document.findStudent.gradYearMenu.value].student[i].userName);
                }
            }
            function redirect(){
              window.location.href = '/default.asp?student?' + document.findStudent.studentMenu.value;
            }
        </script>
    </head>
    <body onload = "populate();">
        <center>
            <table height ="250" width = "555">
                    <tr height = "15" valign = "bottom">
                        <td width = "110">
                            Graduation Year
                        </td>
                        <td>
                            Student Name
                        </td>
                    </tr>
                    <tr valign = "top">
                        <form name = "findStudent" action = "" method = "POST">
                            <td height = "235">
                                <select name = "gradYearMenu" size = "10" width = "150" style = "width: 100; height:200" onclick = "repopulateStudents();">
                                    <option>----------------</option>
                                </select>
                            </td>
                            <td>
                                <select name = "studentMenu" size = "10" width = "150" style = "width: 300; height:200">
                                    <option>----------------</option>
                                </select>
                            </td>
                            <td>
                                <input type = "submit" value = "Find Student" onclick = "redirect()">
                            </td>
                        </form>
                    </tr>
            </table>
        </center>
    </body>
</html>
  1. <html>
  2.     <head>
  3.         <title>Find Student</title>
  4.         <script type="text/javascript"></script>
  5.         <script>
  6.             var year = new Array();
  7.                 year[0] = new gradYear();
  8.                     year[0].gradYear = "2005";
  9.                     year[0].student = new Array();
  10.                         year[0].student[0] = new ident();
  11.                         year[0].student[0].name = "Schrodinger, Cae";
  12.                         year[0].student[0].userName = "schrodingerc";
  13.                         year[0].student[1] = new ident();
  14.                         year[0].student[1].name = "Name, No";
  15.                         year[0].student[1].userName = "namen";
  16.                 year[1] = new gradYear();
  17.                     year[1].gradYear = "2006";
  18.                     year[1].student = new Array();
  19.                         year[1].student[0] = new ident();
  20.                         year[1].student[0].name = "Wiggen, Ender";
  21.                         year[1].student[0].userName = "wiggene";
  22.                 year[2] = new gradYear();
  23.                     year[2].gradYear = "2007";
  24.                     year[2].student = new Array();
  25.                         year[2].student[0] = new ident();
  26.                         year[2].student[0].name = "Clue, No";
  27.                         year[2].student[0].userName = "cluen";
  28.             function gradYear(gradYear, student){
  29.                 this.gradYear = gradYear;
  30.                 this.student = student;
  31.             }
  32.             function ident(name, userName){
  33.                 this.name = name;
  34.                 this.userName = userName;
  35.             }
  36.             function populate(){
  37.                 document.findStudent.gradYearMenu.options.length = 0;
  38.                 for(var i = 0; i <= (year.length - 1); i++){
  39.                     document.findStudent.gradYearMenu.options[i] = new Option(year[i].gradYear, i);
  40.                 }
  41.             }
  42.             
  43.             function repopulateStudents(){
  44.                 document.findStudent.studentMenu.options.length = 0;
  45.                 for(var i = 0; i <= (year[document.findStudent.gradYearMenu.value].student.length - 1); i++){
  46.                     document.findStudent.studentMenu.options[i] = new Option(year[document.findStudent.gradYearMenu.value].student[i].name, year[document.findStudent.gradYearMenu.value].student[i].userName);
  47.                 }
  48.             }
  49.             function redirect(){
  50.               window.location.href = '/default.asp?student?' + document.findStudent.studentMenu.value;
  51.             }
  52.         </script>
  53.     </head>
  54.     <body onload = "populate();">
  55.         <center>
  56.             <table height ="250" width = "555">
  57.                     <tr height = "15" valign = "bottom">
  58.                         <td width = "110">
  59.                             Graduation Year
  60.                         </td>
  61.                         <td>
  62.                             Student Name
  63.                         </td>
  64.                     </tr>
  65.                     <tr valign = "top">
  66.                         <form name = "findStudent" action = "" method = "POST">
  67.                             <td height = "235">
  68.                                 <select name = "gradYearMenu" size = "10" width = "150" style = "width: 100; height:200" onclick = "repopulateStudents();">
  69.                                     <option>----------------</option>
  70.                                 </select>
  71.                             </td>
  72.                             <td>
  73.                                 <select name = "studentMenu" size = "10" width = "150" style = "width: 300; height:200">
  74.                                     <option>----------------</option>
  75.                                 </select>
  76.                             </td>
  77.                             <td>
  78.                                 <input type = "submit" value = "Find Student" onclick = "redirect()">
  79.                             </td>
  80.                         </form>
  81.                     </tr>
  82.             </table>
  83.         </center>
  84.     </body>
  85. </html>
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I'm not sure whether you need to use an absolute path for this as I've never tried a relative one, but I'm pretty sure that the second question mark in '/default.asp?student?' should be an equal sign, so that it's

Code: [ Select ]
http://www.blah.com/page.asp?name=value


rather than

Code: [ Select ]
http://www.blah.com/page.asp?name?value
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

youre thinking PHP... this is ASP...

in ASP you dont assign values to stuff like "page=this&subsection=that"... in ASP its the order that counts... so when it gets the query string it puts it all into an array and you reference it by queryArray(0), queryArray(1) etc... (they should be question marks, its 2 seperate parts of the query string)

the '?student' calling up the student sub-template, and the '?*userName*' is calling up the data for that student... i tried using an absolute path as well as a relitive one ('/default.asp?student?schrodinerc' and 'http://studentserver/default.asp?schrodingerc') they both dont work...
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Sorry, I guess things have changed since I looked at ASP. However, you used to be able to pass values in the same ?name=value manner as in PHP and recover them with request.querystring("name") as I recall. It has been quite awhile though, and I know a lot of things have no doubt changed since asp "classic".
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Since your using javascript redirection I think you probably don't want the form to submit in the traditional manner because I think it's going to conflict with your attempt to customize the querystring even if you change it to GET since the form will try to send all the fields. When I tried just changing the method to get and still using a submit button Opera did the redirection correctly, but FireFox and IE tried to send all the form fields in the url instead of the customized url from the redirect function. When I left the method as post I got a "page not found" error.

I'd suggest substituting a plain button for a submit button to see if it clears up your problem.

Code: [ Select ]
<input type = "button" value = "Find Student" onclick = "redirect()">


I tried it with the old style using request.querystring to recover the value passed by the redirect function and that was the only way I could get it to work. It may not work with the method of retrieving values that you're using, but it's worth a shot anyway.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

thanx that seeems to have fixed it.. i wont be sure until i can get at the server tomarrow and actually test it... but from the files i have here at homa, it seems to work, tyvm

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 163 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.