GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech Show selected dropdown option from SQL? (https://gfy.com/showthread.php?t=1356303)

Publisher Bucks 07-17-2022 08:52 AM

Show selected dropdown option from SQL?
 
Ive seen this done in the past but I dont recall where, so cant find the source code to make it happen.

What I want to do is give a user a drop down option, which they select, it then stores that option in an SQL database.

The next time they return to the page, it shows them what option they chose but also the possibility of changing it in the drop down, for example:

Quote:

<select name=search>
<option value='Google'>Google</option>
<option value='Yahoo'>Yahoo</option>
<option value='Bing'>Bing</option>
<option value='DuckDuckGo'>DuckDuckGo</option>
</select>
Lets say they chose 'Google' as the option.

The next time they hit the page with this dropdown on it, the form automatically shows 'Google' as the chosen option.

How would I go about doing this on a php page, pulling the option they initially chose from the database?

Thanks.

zijlstravideo 07-17-2022 02:05 PM

Get the value from your database:

SQL query... select from xxxx where user etc...
Store it in a variable, for example $usersearchOption = '';


Put the options in an array:

$searchOptions = array('Google','Bing','Yahoo','YoMomma');


Loop through options in array, if a match with the database entry, add "selected":

foreach ($searchOptions as $value)
{
if($value == $usersearchOption) echo "<option selected='selected' value='".$value".'>".$value."</option>";
else {
echo "<option value='".$value."'>".$value."</option>";
} // end of else
} // end loop

Then, add a submit button below and update the new value in database (if the user submitted a change).

Publisher Bucks 07-17-2022 06:01 PM

Quote:

Originally Posted by zijlstravideo (Post 23022629)
Get the value from your database:

SQL query... select from xxxx where user etc...
Store it in a variable, for example $usersearchOption = '';


Put the options in an array:

$searchOptions = array('Google','Bing','Yahoo','YoMomma');


Loop through options in array, if a match with the database entry, add "selected":

foreach ($searchOptions as $value)
{
if($value == $usersearchOption) echo "<option selected='selected' value='".$value".'>".$value."</option>";
else {
echo "<option value='".$value."'>".$value."</option>";
} // end of else
} // end loop

Then, add a submit button below and update the new value in database (if the user submitted a change).

Thank you so much :thumbsup


All times are GMT -7. The time now is 11:59 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc