I have a question about a select box, I have a select box on the client side account.php with a list of States/Provinces and I already have other fields like address, name, email and etc.
I have a col in a database called state thats where user state info stores when I press update account, everything updates perfectly, but I wanna make sure my select box stays selected to whenever a user click on the update button and when they logback in it would stays selected when i choose my state now and update, it will update in the db but on the page when i logback in it goes to the first option box
Any simple example i could have that would be great!
<?php public function getUserData() {
global $db, $core;
$sql = "SELECT *, DATE_FORMAT(created, '%a. %d, %M %Y') AS cdate," . "\n DATE_FORMAT(lastlogin, '%a. %d, %M %Y') AS ldate" . "\n FROM " . $this->uTable . "\n
WHERE id = '" . $this->uid . "'";
$row = $db->first($sql);
return ($row) ? $row : 0;
} ?>
<?php $row = $user->getUserData(); ?>
<select name="state" id="state">
<?php $state = $row['state'];?>
<option value="Ontario" <?php if($state == "Ontario") echo "SELECTED" ?>>Ontario</option>
<option value="Alberta" <?php if($state == "Alberta") echo "SELECTED" ?>>Alberta</option>
</select>
Instead of rendering several
<option>tags, it’s probably easier to use a loop. That makes things easier to maintain too. (You could also retrieve a list of states from the database.)