...

View Full Version : Dropdown Select Box Help



ansonb
03-16-2012, 01:56 AM
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:



$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'>";

Logic Ali
03-16-2012, 02:27 AM
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?



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.

ansonb
03-16-2012, 06:19 AM
because I need both artist_id and artist_name posted to the next page. For example:



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.

ansonb
03-16-2012, 06:21 AM
It is selecting artist id and name from one database, and inserting them into another. So must be seperate values.

2reikis
03-16-2012, 07:35 AM
Is there any reason why the next page can't accept the artist id and query the database for the artist's name?

Logic Ali
03-16-2012, 05:13 PM
because I need both artist_id and artist_name posted to the next page. For example:



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:

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum