SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    grab id from select field

    hey people,

    I want to pass the value of a select option in the url when the user selects an option from a select field without a submit button to help populate another select field automatically.

    The below code worked until I added another form to the same page above it. I've made sure all form names are different so it must be the Javascript.

    Does anyone have a suggestion as to how I could change the javascript to make my page work.

    HTML Code:
    <select name="deptid" id="deptid" class="filter_dropdown" onchange="filter('deptid');">
    HTML Code:
    <script type="text/javascript">
    function filter(which)
    {
     
     //alert(which);
     
     document.forms[0].action="stats.php?deptid="+which;
     document.forms[0].submit();
    }
     
    </script>	

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try an eval :

    Replace :
    document.forms[0].action="stats.php?deptid="+which;
    With :
    eval("document.forms[0].action=\"stats.php?deptid=" + which + "\"");

    I might have missed a quote there so you'll need to check.
    The Eval allows parsing the parameter into the line.
    Thanks,
    Gilad.

    Get your own free personal signature - MyLiveSignature

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the suggestion but i get the same result in the url:

    ../stats.php?deptid=deptid

    is there anyone who maybe able to help with this???
    Last edited by JackRabbitSlims; Sep 4, 2007 at 00:58.

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <select name="deptid" id="deptid" class="filter_dropdown" onchange="filter(this);">
    ???
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've fixed it:

    The javascript was looking for the first form on the page (forms[0])

    document.forms[0].action="stats.php?deptid="+which;
    document.forms[0].submit();

    so i changed it to the name of the form in which the select field was placed:

    forms['dept']

    thanks for your replies

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In other words, what you were really wanting was ...

    HTML Code:
    <script type="text/javascript">
    function filter(which)
    {
     
     //alert(which);
     
     document.forms[which].action="stats.php?deptid="+which;
     document.forms[which].submit();
    }
     
    </script>
    ???
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah sorry for being dumb


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
  •