HTML Select form insert but show selected afterwards

Status
Not open for further replies.

Evilsmoothie

Mad kung-fu artist
Feb 11, 2013
446
57
Hay, so I've been at this for about 20 minutes but I can't figure out how to do it (or what to Google for tbh). So what I'm currently trying to get working is a kind of country setting for a usersystem I've gotten hold of. I want the user to be able to select his/her country from a HTML select form thing and save it. But I also want this setting to show the country chosen earlier if revisiting the setting. Little like radiobuttons, you click one, save, come back and that one is chosen, just with a dropdown with a bagillion countries.

I'm probably unclear, and it's late but I'll try again just in case;
Let's say it's an option to pick your favorite artist:
iMgBCBa.png

You choose out of the list:
kzID9f1.png

You pick for example:
iMgBCBa.png

You save it.
If you change your mind later, you come back and it shows your previous choice as selected:
iMgBCBa.png

I have a slight feeling this is very simple, but I'm not getting it. :tired:
 

JayC

Always Learning
Aug 8, 2013
5,494
1,398
It's saved in the database.. So set the default option to grab from the database :p Make the default before selection = "None"
 

Evilsmoothie

Mad kung-fu artist
Feb 11, 2013
446
57
It's saved in the database.. So set the default option to grab from the database :p Make the default before selection = "None"
Yup okay, that would probably make sense to someone who is smart, but I'm not smart. I got the first two sentences but lost it at the last. Still doesn't help me understand how I would go about this entire thing though. I almost need an example so I guess I should try stackoverflow . :p
 

Bracks0n

New Member
Mar 7, 2015
3
0
I don't really understand what you're trying to do. It would be really confusing UX-wise if I tried to change my country in the settings, then coming back to it and having the country that I had before selected.

It would probably be easier to make a field in your database for last country/countries selected, give a small list under the dropdown and keep the current country selected.
 

JayC

Always Learning
Aug 8, 2013
5,494
1,398
I don't really understand what you're trying to do. It would be really confusing UX-wise if I tried to change my country in the settings, then coming back to it and having the country that I had before selected.

It would probably be easier to make a field in your database for last country/countries selected, give a small list under the dropdown and keep the current country selected.
If you have no idea what he wants to do then don't reply? He doesn't really care if you don't know what you're doing he wants an answer that could help him lead to a possible/logical solution.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
I've no idea what you mean, but if you want it so the new selection is showed upon page refresh/revisit, you will want the selection to be saved in a database.

Then you want a code similar (don't copy and paste it, of course you'll need to modify it) to this:

PHP:
<?php
$user = 2; //the id of the user you are grabbing data from, probably gonna be something like $_SESSION['userid']; fuck knows

$artists = ['Eminem', 'D12', '50 Cent', 'Jay-Z'];

$query = $db->query("SELECT artist FROM `users` WHERE `UserID` = '" . $user . "'");
if ($db->num_rows > 0) {
    $userRow = $query->fetch_array(); //loop through all fields returned from user info
    echo 'Select your favourite artist:<br /><br /><select name="artist">';
 
    foreach ($artists as $artist) {
        echo '<option '; if ($artist === $userRow['artist']) { echo 'selected="selected"'; } echo '</option>';
        //echo all possible artists from $artists array, then do an if statement to
        //see if the current loop value is the same value as the database balue
        //if it is, set the html attribute 'selected' to value 'selected' to make it current value
    }
 
    echo '</select>';
} else {
    echo 'User doesn\'t exist you twat';
}

If it's not what you're asking for, hopefully it helps you arrive to a solution that you need because I think I speak for many people in this thread that I have no idea what you mean

PS: code may not work as I haven't coded PHP in about 3 months?
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
I've no idea what you mean, but if you want it so the new selection is showed upon page refresh/revisit, you will want the selection to be saved in a database.

Then you want a code similar (don't copy and paste it, of course you'll need to modify it) to this:

PHP:
<?php
$user = 2; //the id of the user you are grabbing data from, probably gonna be something like $_SESSION['userid']; fuck knows

$artists = ['Eminem', 'D12', '50 Cent', 'Jay-Z'];

$query = $db->query("SELECT artist FROM `users` WHERE `UserID` = '" . $user . "'");
if ($db->num_rows > 0) {
    $userRow = $query->fetch_array(); //loop through all fields returned from user info
    echo 'Select your favourite artist:<br /><br /><select name="artist">';

    foreach ($artists as $artist) {
        echo '<option '; if ($artist === $userRow['artist']) { echo 'selected="selected"'; } echo '</option>';
        //echo all possible artists from $artists array, then do an if statement to
        //see if the current loop value is the same value as the database balue
        //if it is, set the html attribute 'selected' to value 'selected' to make it current value
    }

    echo '</select>';
} else {
    echo 'User doesn\'t exist you twat';
}

If it's not what you're asking for, hopefully it helps you arrive to a solution that you need because I think I speak for many people in this thread that I have no idea what you mean

PS: code may not work as I haven't coded PHP in about 3 months?
Not that I know anything about PHP but from a HTML perspective instead of using <option selected="selected"> go for <option selected>
 
Status
Not open for further replies.

Users who are viewing this thread

☀️  Switch to Light Theme

Latest posts

Top