Page 1 of 1

option selected

Posted: Tue Jul 11, 2006 11:13 pm
by SidewinderX
I feel really stupid asking this question since it seems like basic html....

So i made a form with a option menu that populates the menu with the information in the database

Code: Select all

$sql41 = "SELECT * FROM ".$prefix."_theme_authors";
	$result41 = $db->sql_query($sql41);
	echo "<form method='index.php' method='get'><select name='designer'>";
	while($row41 = $db->sql_fetchrow($result41)){
	echo "<option value='".$row41['author']."' selected='selected'>".$row41['author']."\n";
	}
	echo "</select><input type='submit' /></form>";
however when the form is submited and the page is refreshed and the new $designer is selected, the menu does not have the proper designer selected, it always selects the last name in the menu.

For example: say my menu contains
John
Bob
Bill
Jim
Chris
Ill select Bob and submit the form, but when the page refreshes Chris is selected. The form is submiting the right value and everything because the address bar will display index.php?designer=Bob. i added the code

Code: Select all

selected='selected'
because thats what i always add in a basic static menu in html to select a particular option....but it dosnt work in php, any help is appreciated.

Thank You.

Re: option selected

Posted: Tue Jul 11, 2006 11:17 pm
by Benjamin
Untested..

Code: Select all

$sql41 = "SELECT * FROM ".$prefix."_theme_authors";
	$result41 = $db->sql_query($sql41);
	echo "<form method='index.php' method='get'><select name='designer'>";
	while($row41 = $db->sql_fetchrow($result41)){
?>
        <option <?php if ((isset($_POST['designer'])) and ($_POST['designer'] == $row41['author'])) { echo 'selected '} ?>value="<?php echo $row41['author']; ?>"><?php echo $row41['author']; ?></option>
<?php
	echo "<option value='".$row41['author']."' selected='selected'>".$row41['author']."\n";
	}
	echo "</select><input type='submit' /></form>";

Posted: Tue Jul 11, 2006 11:41 pm
by SidewinderX
there seems to be an error in here

Code: Select all

<?php if ((isset($_POST['designer'])) and ($_POST['designer'] == $row41['author'])) { echo 'selected '} ?>
ill keep looking but more eyes are better than mine :D

Posted: Tue Jul 11, 2006 11:44 pm
by Benjamin

Code: Select all

$sql41 = "SELECT * FROM ".$prefix."_theme_authors";
	$result41 = $db->sql_query($sql41);
	echo "<form method='index.php' method='get'><select name='designer'>";
	while($row41 = $db->sql_fetchrow($result41)){
?>
        <option <?php if ((isset($_POST['designer'])) and ($_POST['designer'] == $row41['author'])) { echo 'selected '; } ?>value="<?php echo $row41['author']; ?>"><?php echo $row41['author']; ?></option>
<?php
	echo "<option value='".$row41['author']."' selected='selected'>".$row41['author']."\n";
	}
	echo "</select><input type='submit' /></form>";
I forgot a ; try it again.

Posted: Tue Jul 11, 2006 11:51 pm
by SidewinderX
I changed the $_POST's to $_GET's

and removed this line

Code: Select all

echo "<option value='".$row41['author']."' selected='selected'>".$row41['author']."\n";
and it works perfect

Final Code

Code: Select all

$sql41 = "SELECT * FROM ".$prefix."_theme_authors";
        $result41 = $db->sql_query($sql41);
        echo "<form method='index.php' method='get'><select name='designer'>";
        while($row41 = $db->sql_fetchrow($result41)){
?>
        <option <?php if ((isset($_GET['designer'])) and ($_GET['designer'] == $row41['author'])) { echo 'selected '; } ?>value="<?php echo $row41['author']; ?>"><?php echo $row41['author']; ?></option>
<?php

        }
        echo "</select><input type='submit' /></form>";
EDIT:
Cleaned it up a little all the <?php ?><?php got confusing :P

Code: Select all

$sql41 = "SELECT * FROM ".$prefix."_theme_authors";
	$result41 = $db->sql_query($sql41);
	echo "<form method='index.php' method='get'><select name='designer'>";
	while($row41 = $db->sql_fetchrow($result41)){
	echo "<option ";
	if ((isset($_GET['designer'])) and ($_GET['designer'] == $row41['author'])) { echo 'selected '; }
	echo "value='".$row41['author']."'>".$row41['author']."</option>";
	}
	echo "</select><input type='submit' /></form>";
Thanks

Posted: Tue Jul 11, 2006 11:56 pm
by Benjamin
Oh yeah I meant to take that out of there. Glad you got it fixed.