Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql populated listbox

Status
Not open for further replies.

JamesCliff

Programmer
Feb 16, 2005
106
GB
Im using the following code to populate a listbox from a mysql table. I want it so when a value is selected from the listbox its ID is passed to a variable and this variable is used in a link to post the ID to another page. I can get the list to populate and i can select a value from the listbox but i cant figure out howto put the selected rows ID into a variable and use it on the same page just like you would do with a variable formed from a mysql query. This is the code im using:

Code:
          <select name="user_id">
            <?php

$sql2 = "select id, username from brisk_users order by username ASC";
$result2=mysql_query($sql2);
while ( $dropdown = mysql_fetch_array($result2) ){

if ($dropdown["id"]==$id){

echo("<option selected value=". $dropdown["id"]. ">" . $dropdown["username"] . "</option>");
}
else {
echo("<option value=". $dropdown["id"]. ">" . $dropdown["username"] . "</option>");
}
}
?>
          </select>
		  
<?php

$user_id = $user_id[0]

?>
        </p>
        <p>&nbsp;</p>
        <p><font color="#4C4C4C" size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="index.php?page=admin/login/hom3d7r/users/amend_user&user_id=<? echo "$user_id"; ?>">Change user access level</a><br>
          </font></p>
        <p><font color="#4C4C4C" size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="index.php?page=admin/login/hom3d7r/users/del_user&user_id=<? echo "$user_id"; ?>">Delete selected user</a></font></p>

Can someone tell me whats wrong?
 
You cannot achieve that in PHP. Once the page is off the server PHP is out of the scope.
Your question falls into the realm of client side scripting and the JavaScript forum216 would be the best resource on TT to answer your question.
 
wait a min m8....

Im just thinking. So your saying it isnt possible to store the id of a selected row in a listbox into a variable?

Omg.... im doomed. Ive been designing a CMS in php and mysql all along thinking that i could store a values id into a variable when selected in a listbox. I could then use this variable in mysql querys. Ive got the user side of the site fully built and constructed and now im implementing the admin site of the site. I was planning to use this method for most of the content adding under differect sections and alot of other stuff.

Is there any other way i could achieve what im trying to do above in php?
 
You can put the selected item from a listbox into a variable if you submit the page to a PHP script that takes the values posted or supplied by a GET string. However, constructing a "link" with GET parameters would have to be done client side.

You are not doomed, if you were, all of the PHP community were doomed.

In order for PHP to work with the supplied values they must be sent to the server. That's why there are HTML forms with form elements. You can e.g. POST the form to the same script and have a conditional section at the top of the script which is executed when values are posted.
 
yeh, sorted it now m8.

I dont know what i was thinking lol.
I put the listbox inside a form and submitted the form to the same script and used a conditional section like you suggested when the form is submitted. This then updates the links.

It works fine

Thanks

Jim

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top