Echo posted variable off of select option

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
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Echo posted variable off of select option

Post by marnieg »

I have a form that has some fields that are drop downs populated from other tables. If the user gets an error when trying to add the record I echo my fields on the form so they don't have to enter them again. I have this working on all my fields that are of type text, textarea and checkbox, but can't get the ones with drop downs to echo. Here is a snippet of my code.

Code: Select all

<td><select name="state_abbr" [b]<?php if($state_abbr): ?>value='<?php echo $state_abbr; ?>'<?php endif; ?>[/b]>
	<?php
		$statequery = 'SELECT abbr,name FROM state ORDER BY seq,abbr';
		$stateresult = mysql_query($statequery, $conn);
 		while($staterow = mysql_fetch_array($stateresult, MYSQL_ASSOC))
		{
			$statelevel = $staterow['abbr'];
			$statename = $staterow['name'];
			print("<option value='$statelevel'>$statelevel - $statename</option>");
		}
	?>
        </select>
</td>
There area in bold is where I'm trying to echo the value of the state they already selected. Here is my code for checkbox fields.

Code: Select all

<td><input name='active' type='checkbox' <?php if($active): ?>checked="checked"<?php endif; ?>/></td>
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Echo posted variable off of select option

Post by PHPHorizons »

Hello marnieg,

Select elements use the selected attribute. You can just put selected, or if using XHTML, you can put selected="selected".

Hope that helps.
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: Echo posted variable off of select option

Post by marnieg »

Tried using "selected" like this but still does not echo the state value that was selected by the user.

Code: Select all

<td><select name="state_abbr" <?php if (strlen($state_abbr) > 0): ?>selected="selected"<?php endif; ?>
	<?php
		$statequery = 'SELECT abbr,name FROM state ORDER BY seq,abbr';
		$stateresult = mysql_query($statequery, $conn);
 		while($staterow = mysql_fetch_array($stateresult, MYSQL_ASSOC))
		{
			$statelevel = $staterow['abbr'];
			$statename = $staterow['name'];
			print("<option value='$statelevel'>$statelevel - $statename</option>");
		}
	?>
        </select>
</td>

I think it is executing the query statements and reloading the Option values again. Somehow need to bypass this I think if already selected.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Echo posted variable off of select option

Post by PHPHorizons »

Ah, marnieg you're close. That selected deal needs to be applied to the OPTION tag that was selected, not the SELECT tag itself.

Let me know if that doesn't work out for ya. Cheers
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: Echo posted variable off of select option

Post by marnieg »

Thanks for pointing me in the right direction. Here is the code that works.

Code: Select all

<td><select name="state_abbr" 
	<?php
		$statequery = 'SELECT abbr,name FROM state ORDER BY seq,abbr';
		$stateresult = mysql_query($statequery, $conn);
 		while($staterow = mysql_fetch_array($stateresult, MYSQL_ASSOC))
		{
			$statelevel = $staterow['abbr'];
			$statename = $staterow['name'];
			if ($_POST['state_abbr'] == $statelevel)
			{
			print("<option selected='selected'>$statelevel - $statename</option>");
			}
			else
			{
			print("<option value='$statelevel'>$statelevel - $statename</option>");
			}
		}
	?>
        </select>
</td>
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: Echo posted variable off of select option

Post by marnieg »

Needed to add "value" on the "option" tag also so that I could test this field in my form validation requiring user to select a value from the drop down.

Code: Select all

<td><select name="state_abbr" 
	<?php
		$statequery = 'SELECT abbr,name FROM state ORDER BY seq,abbr';
		$stateresult = mysql_query($statequery, $conn);
 		while($staterow = mysql_fetch_array($stateresult, MYSQL_ASSOC))
		{
			$statelevel = $staterow['abbr'];
			$statename = $staterow['name'];
			if ($_POST['state_abbr'] == $statelevel)
			{
			print("<option value='' selected='selected'>$statelevel - $statename</option>");
			}
			else
			{
			print("<option value='$statelevel'>$statelevel - $statename</option>");
			}
		}
	?>
        </select>
</td>
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Echo posted variable off of select option

Post by PHPHorizons »

Yeah, that's the way to do it ;)
One thing I would probably do, and you have already done half of this, is to put the value in the selected option the same way as it would be if the option weren't selected. So rather than value='', I would actually go ahead and fill in that value.

Code: Select all

print("<option value='$statelevel' selected='selected'>$statelevel - $statename</option>");
Cheers
Post Reply