Jump to content

<SELECT> With PHP


jbonnett

Recommended Posts

Hi all,

Im trying to get the requested genre and compare it with the <SELECT> list to add the word SELECTED on the option.

So if the requested genre is the same as the option name them make that the SELECTED option.

 

Cheers.

 

Here's a bit of code I made that does not work

<? 
function selected(){
if ($_REQUEST['genre'] = $name){
echo"SELECTED";
}}
?>

Genre:
<SELECT class="sort" align="right" onChange="window.location.href=this.options[this.selectedIndex].value;">
<option value="<?=$_SERVER['PHP_SELF'];?>" <?$name = ''; selected(); ?>>Any</option>
<option value="<?=$_SERVER['PHP_SELF'];?>?genre=Action" <?$name = 'Action'; selected(); ?>>Action</option>
<option value="<?=$_SERVER['PHP_SELF'];?>?genre=Adventure" <?$name = 'Adventure'; selected(); ?>>Adventure</option>
<option value="<?=$_SERVER['PHP_SELF'];?>?genre=Animation" <?$name = 'Animation'; selected(); ?>>Animation</option>
</SELECT>

Link to comment
https://forums.phpfreaks.com/topic/260031-with-php/
Share on other sites

Your IF statement is setting the variable, not checking it. Also, in your HTML, when calling the PHP function, you need to pass the $name with it to the function. Like so:

 

<?php

function selected($name){
if ($_REQUEST['genre'] == $name){
echo"SELECTED";
}}
?>

Genre:
<SELECT class="sort" align="right" onChange="window.location.href=this.options[this.selectedIndex].value;">
<option value="<?=$_SERVER['PHP_SELF'];?>" <?php $name = ''; selected($name); ?>>Any</option>
<option value="<?=$_SERVER['PHP_SELF'];?>?genre=Action" <?php $name = 'Action'; selected($name); ?>>Action</option>
<option value="<?=$_SERVER['PHP_SELF'];?>?genre=Adventure" <?php $name = 'Adventure'; selected($name); ?>>Adventure</option>
<option value="<?=$_SERVER['PHP_SELF'];?>?genre=Animation" <?php $name = 'Animation'; selected($name); ?>>Animation</option>
</SELECT>


Link to comment
https://forums.phpfreaks.com/topic/260031-with-php/#findComment-1332837
Share on other sites

Try this new and improved code:

 

<?php

function selected($name){
if(isset($_REQUEST['genre'])){
if ($_REQUEST['genre'] == $name){
echo"SELECTED";
}}
}
?>

Genre:
<SELECT class="sort" align="right" onChange="window.location.href=this.options[this.selectedIndex].value;">
<option value="<?php echo$_SERVER['PHP_SELF'];?>" <?php $name = ''; selected($name); ?>>Any</option>
<option value="<?php echo $_SERVER['PHP_SELF'];?>?genre=Action" <?php $name = 'Action'; selected($name); ?>>Action</option>
<option value="<?php echo $_SERVER['PHP_SELF'];?>?genre=Adventure" <?php $name = 'Adventure'; selected($name); ?>>Adventure</option>
<option value="<?php echo $_SERVER['PHP_SELF'];?>?genre=Animation" <?php $name = 'Animation'; selected($name); ?>>Animation</option>
</SELECT>



Link to comment
https://forums.phpfreaks.com/topic/260031-with-php/#findComment-1332845
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.