option selected

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

option selected

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: option selected

Post 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>";
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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
Last edited by SidewinderX on Tue Jul 11, 2006 11:57 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Oh yeah I meant to take that out of there. Glad you got it fixed.
Post Reply