Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change the selected option in select tag with php

Status
Not open for further replies.

tiamat2012

Programmer
Dec 12, 2004
144
US
is it possible?

I'm getting a bunch of values back from a database, but many of the values belong select tags, and they should simply be selected.

The only way I can think of it is putting them into hidden form elements and then using javascript to do the rest, but I've heard that ASP and ASP .NET can do it, so I was wondering.

Thanks
-Kerry
 
PHP can programmatically pre-select an option of a SELECT tag.

However, it can only do it as your script is producing the HTML that causes the browser to render the page. Once the HTML has been sent to the browser, PHP can't respond to user input and then programatically change the selected option.



Want the best answers? Ask the best questions! TANSTAAFL!
 
That's exactly what I need, I'm doing it on a post from another page, so everything needs to be selected before the page loads.

How do I do it?

Thanks,
-Kerry
 
Your script should output the string "selected" within the <option> tag that should be preselected:

Code:
<?php

$option_to_preselect = 3;

$options = array
(
	1 => 'adam',
	2 => 'betty',
	3 => 'charles',
	4 => 'denise',
	5 => 'eric'
);

print '<html><body>
<form method="post" action="show_post.php">
<select name="foo">';

foreach ($options as $index => $value)
{
	print '	<option value="' . $index . '"';
	
	if ($index == $option_to_preselect)
	{
		print ' selected ';
	}
	
	print '>' . $value . '</option>
';
}

print '</select>
<input type="submit"></form></body></html>';
?>

produces:

[tt]<html><body>
<form method="post" action="show_post.php">
<select name="foo"> <option value="1">adam</option>
<option value="2">betty</option>
<option value="3" selected >charles</option>
<option value="4">denise</option>
<option value="5">eric</option>
</select>
<input type="submit"></form></body></html>[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
The only problem with that is I already have the select tag hard coded, is there anyway to get the object and change it?

Another option would be to do a switch on the database entry, but that is still a lot more work than other possibilities.

-Kerry
 
I have a select like


Code:
<select name="location" id="location">
<option value="Hawaii">Hawaii</option>
<option value="Los Angeles">Los Angeles</option>
<option value="New York">New York</option>
<option value="Tampa">Tampa</option>

except I have many of these select statements, and I pull from the database which value has been selected before, like "Los Angeles", and so in PHP I want to grab the object or somehow insert "selected" into that option so that it comes up with the right data.

-Kerry
 
ideally you should redesign your code to generate the locations dynamically but failing that:

Code:
$theselectedvalue = "Los Angeles";
$theselectbox = <<<EOL
<select name="location" id="location">
<option value="Hawaii">Hawaii</option>
<option value="Los Angeles">Los Angeles</option>
<option value="New York">New York</option>
<option value="Tampa">Tampa</option>
</select>

EOL;
$searchterm = '<option value="'.$theselectedvalue.'"';
$theselectbox = str_replace($searchterm, $searchterm . " selected ", $theselectbox);

for dynamic select boxes
Code:
$theselectedvalue = "Los Angeles";
$locations = array("Hawaii", "Los Angeles", "New York", "Tampa");
$selectbox = '<select name="location" id="location">';
foreach ($locations as $location):
  $selected = (strtolower($theselectedvalue) == strtolower($location)) ? " selected " : "";
  $selectbox .= <<<EOL
<option value="$location" $selected >$location</option>

EOL;
endforeach;
$selectbox .= "</select>";
echo $selectbox
;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top