Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    36
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Arrow Dropdown Select Box Help

    I hope someone can help me. I am having a problem with a dropdown selection box. I think Javascript is the way to solve it, but I am unfamiliar with the language, any help would be much appreciated.

    My drop down box selection box is created dynamically with data from a mysql database.

    It is a list of artists/musicians. I have 1 column in the db table called artist_name and another called artist_id. The sql query selects both of these columns and displays artist names in the select box.

    What I need is for the artist_id to be sent along with whichever artist is selected in the select box to the next page upon submitting the form.

    I put in a input field, which I will make hidden eventually. But I want the input field to change to whatever the correct id is when the select box is changed.

    I am thinking that the way to do this would be with a javascript onchange event. Will this work ?

    If so I need help with the code. I have searched for examples online and while somewhat helpful, I am such a newbie with Javascript that I could not figure out how to modify the examples to what I need.

    Here is the php/mysql code I have so far:

    PHP Code:
    $query4 "SELECT artist_id,artist_name FROM artists ORDER BY artist_name";      
    $result4 mysql_query($query4) or die(mysql_error());

    echo 
    "<select name='artist'>
         <option value=''>Select Artist</option>"
    ;
         while(
    $row=mysql_fetch_array($result4))
           {
            
    $artist_id=$row['artist_id'];
                echo 
    "<option value='"$row['artist_name'] ."'>"$row['artist_name'] ."</option>";
            }
    echo 
    "</select>
         <input type='text' name='artist_id' id='artist_id' size='5' class='textfield' value='$artist_id'>"


  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by ansonb View Post
    What I need is for the artist_id to be sent along with whichever artist is selected in the select box to the next page upon submitting the form.
    You're reading into $artist_id on each iteration of the loop, but not using the value anywhere until the loop finishes, when you save the last value in the text field. Why can't the value parameter of each option store the id?

    PHP Code:
    while($row=mysql_fetch_array($result4))
    {
     echo 
    "<option value = '"$row['artist_id'] ."'>"$row['artist_name'] ."</option>";

    Then there's no need for any scripting or text field, the data just gets sent in the normal way.
    Last edited by Logic Ali; 03-16-2012 at 02:30 AM.

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    36
    Thanks
    5
    Thanked 0 Times in 0 Posts
    because I need both artist_id and artist_name posted to the next page. For example:

    PHP Code:
    while($row=mysql_fetch_array($result4)) 

     echo 
    "<option value = '"$row['artist_id'] . $row['artist_name'] ."'>"$row['artist_name'] ."</option>"

    But if I do it this way, It will post to the next page as 1 value. For example a value would be:

    17AdamFielding

    I need those 2 values seperated on the next page.
    Just not sure how I would seperate them again if I did it this way.

    The only other way I could think of to do this is to have the id in a seperate input box that would reflect the id when the select box is changed. Then just set the input type to "hidden" so it wouldn't actually show on the page.

    That way they would post to the next page as 2 seperate values.

  • #4
    New Coder
    Join Date
    Feb 2012
    Posts
    36
    Thanks
    5
    Thanked 0 Times in 0 Posts
    It is selecting artist id and name from one database, and inserting them into another. So must be seperate values.

  • #5
    Regular Coder 2reikis's Avatar
    Join Date
    Nov 2005
    Location
    California
    Posts
    167
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Is there any reason why the next page can't accept the artist id and query the database for the artist's name?
    2Reikis
    I did not arrive at my understanding of the physical universe through my rational mind.
    Albert Einstein

  • #6
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by ansonb View Post
    because I need both artist_id and artist_name posted to the next page. For example:

    PHP Code:
    while($row=mysql_fetch_array($result4)) 

     echo 
    "<option value = '"$row['artist_id'] . $row['artist_name'] ."'>"$row['artist_name'] ."</option>"

    But if I do it this way, It will post to the next page as 1 value. For example a value would be:

    17AdamFielding
    Then you can insert a delimiting character like '|', which the receiving page can use to split the two values:
    PHP Code:
    while($row=mysql_fetch_array($result4)) 

     echo 
    "<option value = '"$row['artist_id'] . "|" $row['artist_name'] ."'>"$row['artist_name'] ."</option>"



  •  

    Posting Permissions

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