Page 1 of 1

Echo posted variable off of select option

Posted: Tue Aug 17, 2010 2:27 pm
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>

Re: Echo posted variable off of select option

Posted: Tue Aug 17, 2010 3:06 pm
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.

Re: Echo posted variable off of select option

Posted: Tue Aug 17, 2010 3:52 pm
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.

Re: Echo posted variable off of select option

Posted: Tue Aug 17, 2010 7:46 pm
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

Re: Echo posted variable off of select option

Posted: Wed Aug 18, 2010 8:41 am
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>

Re: Echo posted variable off of select option

Posted: Wed Aug 18, 2010 9:42 am
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>

Re: Echo posted variable off of select option

Posted: Wed Aug 18, 2010 11:18 am
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