Solved

Change selected value of drop down list array.

Posted on 2004-10-24
22,261 Views
Last Modified: 2012-08-13
I am createing an attendance management system for a school in PHP and have come into some trouble.

On the registers form, i have an array of drop down lists called "mark" and each ones index is the unique number of the pupil (e.g. "U56546546456").

I need some javascript that enable you to change one drop down list, and apply that change to all the others in the array. A sort of "Master Drop Down List". This is to save teachers time in setting every student to present as they can flood fill all the drop down list with the mark "P".

How can i do this in javascript which i presume is the language i need to use?

Thanks
0
Question by:adamday
    6 Comments
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    not sure exactly how evreything is set up her but this perhaps?

    <head>
    <script>
    function init(){
     studSel=new Array()
     studSel['U56546546456']=document.f1.s1
     studSel['U56546546468']=document.f1.s2
     studSel['U56546546473']=document.f1.s3

    }
    function setPresent(){
     for (i in studSel) studSel[i].selectedIndex=1
    }
    </script>
    </head>

    <body onload="init()">
    <p><input type="button" value="set all present" onclick="setPresent()"> </p>
    <form name="f1">
      <p><select name="s1" size="1">
        <option>-Choose-</option>
        <option>P</option>
        <option>A</option>
      </select> <select name="s2" size="1">
        <option>-Choose-</option>
        <option>P</option>
        <option>A</option>
      </select> <select name="s3" size="1">
        <option>-Choose-</option>
        <option>P</option>
        <option>A</option>
      </select> </p>
    </form>
    0
     
    LVL 3

    Expert Comment

    by:nitrogenx
    I assume you can output the javascript function MasterChanged() using PHP
      <script>
      function MasterChanged()
      {
        document.form1.s1.selectedIndex=
        document.form1.s2.selectedIndex=
        document.form1.s3.selectedIndex=
        document.form1.master.selectedIndex;    
      }
      </script>
        <form id="form1" name="form1">
       
     
      Master:
      <select name="master" onchange="MasterChanged()" size="1">
        <option>-Choose-</option>
        <option>P</option>
        <option>A</option>
      </select>
      Student one:
      <select name="s1" size="1">
        <option>-Choose-</option>
        <option>P</option>
        <option>A</option>
      </select>
      Student two:
      <select name="s2" size="1">
        <option>-Choose-</option>
        <option>P</option>
        <option>A</option>
      </select>
      Student three:
      <select name="s3" size="1">
        <option>-Choose-</option>
        <option>P</option>
        <option>A</option>
      </select>    
        </form>
    0
     
    LVL 3

    Expert Comment

    by:nitrogenx
    i missed the start form tag there:   <form name="form1">
    0
     

    Author Comment

    by:adamday
    Thanks for the quick replies,

    The above code is the way i think i need to solve this proble, however, the register marks drop down lists, are in an array. e.g.

    <select name=mark[U894378547353]>
    <option ....
    </select>
    <select name=mark[A234479879499]>
    <option ....
    </select>

    So i tried the javascript:

    echo("<script language='javascript'>");
    echo("function setpresent() {");
    while (!$rs->EOF) {
    $student=$rs->Fields("UPN");
    echo("document.register.mark['$student->value'].selectedindex=1;");
    echo("\n");
    $rs->MoveNext();
    }
    echo("} \n</script>");

    The field UPN stands for Unique Pupil Number. "register" is the name of the form.

    When i try this, i get an error saying:

    'document.register.mark.U43543543543' is null or not an object

    How can i fix this

    Thanks Again
    0
     
    LVL 3

    Accepted Solution

    by:
    its not working because javascript thinks that mark[A234479879499] is refering to an array rather than a form name.  Either change it to mark_A234479879499 or something or replace the

    echo("document.register.mark['$student->value'].selectedindex=1;");

    with

    echo("document.getElementsByName('mark["."$student->value"."]')[0].selectedIndex=1;");
    I think thats correct...the javascript line should be rendered as:

    document.getElementsByName('mark[U894378547353]')[0].selectedIndex=1;
    0
     
    LVL 3

    Expert Comment

    by:nitrogenx
    *form element name
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Freelancers to Complete JavaScript Projects

    Source the talented Expert Exchange community
    for top quality work on your JavaScript projects.

    Hire the best. Collaborate easily. Get quality work.

    Suggested Solutions

    A common challenge, or question, when working with two calendar dates is: What is the difference between the specified days? As is frequently the case, the answer is: It depends... What kind of difference do you want? Given two date obje…
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved." Ralph Waldo Emerson Introduction: One of the wonderful things about the web is that it makes it s…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now