SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Automatic Field Selection

    I have created a select menu where the user chooses from a list of peoples names stored in a database.

    There is also another select menu that shows the age of each person.

    Basically what I want to do is as follows:

    When the user selects a person's name from the selectmenu, the age selectmenu should then automatically display the age relevant to the selected person's name.

    Make sense?

    Thanks,

    Kevin.

  2. #2
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This can be done through accomplished through JavaScript.
    Provided you populate the select fields such that each positions values correspond (i.e. 1st value in Name Select field corresponds to 1st value in Age Select field). Simply get the selectedIndex of the Name Select field and changet the selected index of the Age Select field to that.

    If however, your Age Select field is sequential (i.e. 1, 2, 3, 4, 5), then to use JS , you would need to create an array or some such in JS through the PHP script. The other alternative, if you don't wish to have JS arrays, is to have the page submit on changes to the Name Select value, and then on re-rendering, select the appropriate age.

    Hope I made sense somewhere in that jumble.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is my code so far...These are the select menus in the form. Basically what I want is this...the user selects the course menu and based on their selection the date menu is automatically filled with the date linked to the course selection.

    Make sense?

    PHP Code:
     <label for="course2">Course:</label>
     <select name="course">
     <option value="none" onchange="optChange()">Select Course</option>
     <?php
     
    while ($row mysql_fetch_array($result))
     {
     echo 
    "<option value='" $row["course_ID"] . "'>" $row["course"] . "</option>";
     }
     
    ?>
     </select><br /><br />
     
     <label for="date2">Date:</label>
     <select name="date">
     <option value="none">Select Date</option>
     <?php
     
    while ($row mysql_fetch_array($result2))
     {
     echo 
    '<option value="'.$row['date_ID'].'">' .$row['date']. '</option>';
     }
     
    ?>
     </select>
    And here is my JavaScript so far...

    Code:
     <script language="JavaScript" type="text/javascript">
     <!--
     function optChange() {
     document.form.date.selectedIndex = document.form.course.selectedIndex;
     }
     //-->
     </script>
    Why is it not working?

    thanks,

    Kevin.

  4. #4
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you say it's not working, what do you mean?
    Do you mean that the function doesn't fire? Or that the function doesn't do what's its supposed to do?

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The date select menu isn't automatically selected.

    Any ideas?

  6. #6
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My JS is a bit off... so can't say exactly what's going on.
    Could you try this and see if an alert pops up?
    Code:
    <script language="JavaScript" type="text/javascript">
     <!--
     function optChange() {
    alert(document.form.course.selectedIndex);
     document.form.date.selectedIndex = document.form.course.selectedIndex;
     }
     //-->
     </script>

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The alert doesn't work. Any ideas what is wrong??

    thanks,

    Kevin.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •