开发者

populating drop-down menu in php from mysql database

开发者 https://www.devze.com 2023-03-18 12:02 出处:网络
I have managed to fill a drop down menu in php, populating it with info from mysql database. However, I do not know how to have it do something when I click on a member of the drop down menu. I am not

I have managed to fill a drop down menu in php, populating it with info from mysql database. However, I do not know how to have it do something when I click on a member of the drop down menu. I am not that familiar with php. Here is the code I have to fill the drop-down menu

$res=mysql_query("SELECT * FROM classesToUsers order by UserID"); 
echo "<select name=myselect>"; 
while($row=mysql_fetch_assoc($res)) { 
   echo "<option value=$r开发者_C百科ow[ClassID]>$row[UserID]</a></option>"; 
} 
echo "</select>";

With classesToUsers being my table in my db. My question is, how do I have it do something, such as insert something into the database, when the user clicks on a member of the dropdown menu?


You have a few options. If you are just learning, I would recommend avoiding AJAX for now and just get the feel of adding a link to a page which makes a db insert.

$res=mysql_query("SELECT * FROM classesToUsers order by UserID"); 
echo "<select name=myselect>"; 
while($row=mysql_fetch_assoc($res)) { 
   echo '<option value="'.$row['ClassID'].'"><a href="add_user.php?userID='.$row['UserID'].'">'.$row['UserID'].'</a></option>'; 
} 
echo "</select>";

Then make a php page called add_user.php:

if(!empty($_GET['UserID']))
{
    // Add $_GET['UserID'] to db...
}

Keep in mind this is NOT secure - never do DB INSERTS off of $_GET data on a public website as it exposes you to cross site scripting exploits (XSS), but for learning purposes, this is very helpful. After this, Convert that link into a form and $_POST it to your add_user.php page.

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消