[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 22355
  • Last Modified:

Change selected value of drop down list array.

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
adamday
Asked:
adamday
  • 4
1 Solution
 
GwynforWebCommented:
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
 
nitrogenxCommented:
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
 
nitrogenxCommented:
i missed the start form tag there:   <form name="form1">
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
adamdayAuthor Commented:
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
 
nitrogenxCommented:
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
 
nitrogenxCommented:
*form element name
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now