jbonnett Posted March 30, 2012 Share Posted March 30, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/260031-with-php/ Share on other sites More sharing options...
batwimp Posted March 30, 2012 Share Posted March 30, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/260031-with-php/#findComment-1332837 Share on other sites More sharing options...
jbonnett Posted March 30, 2012 Author Share Posted March 30, 2012 I forgot about passing the variable although it still does not work . Quote Link to comment https://forums.phpfreaks.com/topic/260031-with-php/#findComment-1332838 Share on other sites More sharing options...
batwimp Posted March 30, 2012 Share Posted March 30, 2012 How doesn't it work? Error messages? Quote Link to comment https://forums.phpfreaks.com/topic/260031-with-php/#findComment-1332839 Share on other sites More sharing options...
jbonnett Posted March 30, 2012 Author Share Posted March 30, 2012 Does not add the "SELECTED" to anything and no error messages :-\. Quote Link to comment https://forums.phpfreaks.com/topic/260031-with-php/#findComment-1332840 Share on other sites More sharing options...
batwimp Posted March 30, 2012 Share Posted March 30, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/260031-with-php/#findComment-1332845 Share on other sites More sharing options...
jbonnett Posted March 30, 2012 Author Share Posted March 30, 2012 Thank you so much it works, I been trying to figure this out all day Quote Link to comment https://forums.phpfreaks.com/topic/260031-with-php/#findComment-1332846 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.