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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.