SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing additional variables from a drop-down menu selection?

    Let's say I have this simple construct:

    <select name="first">
    <option value="bob">bob</option>
    <option value="fred">fred</option>
    <option value="mary">mary</option>
    </select>

    but I also know that

    bob's age is 27
    fred's age is 31
    mary's age is 23

    So when the user selects "bob" from the drop down list, I actually want to pass two variables to the next page:
    first=bob (which is taken care of by the select, no probs)
    age=27

    I think I need a bit of javascript to be able to send the second variable, perhaps in the URL?

    Thanks.
    Last edited by spaceman; Nov 15, 2001 at 20:02.
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  2. #2
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where is the age value coming from? I mean if it is based on the value "bob" then you should be able to get the information on the next page from the same source.

    Are you getting the info from a database? You could easily query the data based on "bob" on your form action page.

    Maybe some more code would clear this up. I know what you are asking, but I want to know what the source of the "age" values is.

  3. #3
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're right - the data is coming from a MySQL database, and I can quite easily do a query on the 'receiving' page, but seeing as I've already done a query on the first page to get the secondary age variables, I was trying to find away to avoid re-accessing the database for the same info.

    You know, I'm sure I had a bit of javascript a while ago which did the job, but I can't for the life of me remember which site I put it in, and I haven't been able to find any reference to it in the major forums.

    No worries - there's always more than one way to skin a cat . So in the meantime my solution has been to put both values in the same variable, eg.

    <select name="first">
    <option value="bob|27">bob</option>
    <option value="fred|31">fred</option>
    <option value="mary|23">mary</option>
    </select>

    ...and then on the receiving page I use PHP to separate out the result:

    list($first,$age)=explode("|",$first);

    OK, so this avoids me having to re-lookup the age data from the database for the selected name, BUT it would be slicker if I didn't have to do any additional coding on the receiving page at all. Hence the javascript request in my original post. I really must get around to fully learning javascript one day instead of always relying on re-using other people's code
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  4. #4
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two things:

    1) If you do your stuff server-side, then you can eliminate client errors and old browsers that might not like javascript code.

    2) The way you are doing things now seems to look like a pretty good solution. Why do you want to change it?

    I guess you could always write a function where OnSelect you could grab the age data. But you would probably need to set up an array of some sort, which is a lot more complex than the status quo.


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
  •