3

I have created two links where I would like the page contents to change. The problem is the URL changes but not the page content.

<h3>Filter Results</h3>
<p><a href="index.php?filter='Action'>Action</a></p>
<p><a href="index.php?filter='Comedy'">Comedy</a></p>
if (isset($_GET['filter']) == 'Action') {
    echo 'Action';  
}
else if (isset($_GET['filter']) =='Comedy') {    
    echo 'Comedy';
}

It always outputs the first link information "Action".

10 Answers 10

7

Your links are faulty:

<p><a href="index.php?filter=Action">Action</a></p>
<p><a href="index.php?filter=Comedy">Comedy</a></p>
<!--                         ^    ^ No single quotes (' ') -->

Yogesh Suthar pointed it out first

Also, isset() will return a boolean (true or false; based on whether or not the variable is set). You're comparing a boolean to a string, a string will always be converted into TRUE (unless the string is "false" or similar), so basically, if the variable is set, the first condition will always match.

You want

if (isset($_GET["filter"]) && $_GET["filter"] === "Action")

Note the use of ===, this will make sure that the variable is exactly what you think it is, and not some sort of other type variable.

Few more points (Shamelessly stolen taken from other answers)

  • If there are multiple possible filters, check for the variable existance once, and use a switch/case block to determine which of them it is:

    if(isset($_GET['filter'])) {
        switch($_GET['filter']) {
            case 'Action':
                echo 'Action';
                break;
            case 'Comedy':
                echo 'Comedy';
                break;
        }
    }
    
Sign up to request clarification or add additional context in comments.

5 Comments

Yeah posting this in the chatroom.. oh well that's clever anyway :)
@Herbert: Seeing as my answer is (now was) the top voted answer, I wanted to include all the goodness from the other answers, so that OP doesn't need to read it in fragments.
Yeah, I was j/k. I wish more people would do that and tidy up the place a bit. :)
There is a typo in your HTML. You are missing a double quote in the first line after Action
@byf-ferdy: You're right. Next time, feel free to suggest an edit and I will accept it :)
5

The function isset will only check if the variable is existing! It will not return its value! Try this instead:

<h3>Filter Results</h3>
<p><a href="index.php?filter=Action">Action</a></p>
<p><a href="index.php?filter=Comedy">Comedy</a></p>
if(isset($_GET['filter']) && $_GET['filter'] == 'Action'){
    echo 'Action';  
}

else if(isset($_GET['filter']) && $_GET['filter'] == 'Comedy') {
    echo 'Comedy';
}

Also, using switch might make things easier in the future:

<h3>Filter Results</h3>
<p><a href="index.php?filter=Action">Action</a></p>
<p><a href="index.php?filter=Comedy">Comedy</a></p>
if(isset($_GET['filter'])) {
    switch($_GET['filter']) {
        case 'Action':
            echo 'Action';
            break;
        case 'Comedy':
            echo 'Comedy';
            break;
    }
}

4 Comments

@Lars Ebert the Url changes but nothing is outputted to the page as aspected
@SamuelHughesMensah I changed the link-urls, there were some errors. See this answer: stackoverflow.com/a/17877676/2580794
@LarsEbert. The code is 100% working. Thanks. I failed to copy it well. Sorry bro
@SamuelHughesMensah Then go ahead and mark one of the answers as a solution!
3

As @MadaraUchiha said about isset and,

if(isset($_GET['filter']) == 'Action')

should be

if(isset($_GET['filter']) && $_GET['filter'] == 'Action')

Also

<a href="index.php?filter='Action'>Action</a>
        ^                 ^      ^ // here you started " but not ended and remove the single quotes around Action

should be

<a href="index.php?filter=Action">Action</a>

1 Comment

Thanks Worked Like Magic
3

Make sure that you insert an opening and a closing php tag: <?php and ?> To simplify it a bit you could just echo the value you get via $_GET

<h3>Filter Results</h3>
<p><a href="index.php?filter='Action'>Action</a></p>
<p><a href="index.php?filter='Comedy'>Comedy</a></p>
<?php
    if(isset($_GET['filter'])){
        echo $_GET['filter'];  
    }
?>

Comments

2

The function isset will return true or false (it checks whether the variable is set or not). Change your code:

if(isset($_GET['filter']) && $_GET['filter'] == 'Action') {

2 Comments

Nothing is being ouputted ot the page
You should remove the single quotes in the href of your links. ...?filter=Action.
1

Your if condition is not correct do it:

if(isset($_GET['filter']) && $_GET['filter'] == 'Action'){
  echo 'Action';  
}

Similarly with else if:

else if(isset($_GET['filter']) && $_GET['filter'] =='Comedy') {

As you are comparing the isset($_GET['filter']) with the value although isset returns true of false so you need to compare the value of $_GET['filter'].

Comments

1

you dont have to use isset() and then compare.

$filter = $_GET['filter'];

if(isset($filter)){
   if($filter == 'Action'){
     echo 'Action';
   }else if($filter == 'Comedy'){
     echo 'Comedy';
   }

}

Comments

1

isset returns true and since 'Action' is not null, it evaluates to true.

if ((isset($_GET['filter'])) && ($_GET['filter'] == 'Action')) {
    // ...
} else if ((isset($_GET['filter'])) && ($_GET['filter'] == 'Comedy')) {
    // ...
}

BTW such code would sooner or later become a nightmare to maintain.

You could instead, for example

function preventDirectoryTraversal($requestParam) {
    return preg_replace("/\//", "", $requestParam);
}

// ...

if (isset($_GET['filter'])) {
    $filterName = preventDirectoryTraversal($_GET['filter']);
    include(FILTERS_DIR . "/" .  $filterName . ".php");
}

or something alike. Of course this can be further improved, but I hope you get the point.

4 Comments

What will be easiest way to write this code. I am new to PHP. thanks
@SamuelHughesMensah Well, the easy approach is not always a good one, but the above with the include seems to be sufficient for now. Then, you define a FILTERS_DIR constant that is the name of the folder into which you put the filters (actually, with filters, filter chaining is a good solution, but is out of scope for now). The files would be named filterName.php and based on the filter name, you include the appropriate functionality. Before this, you need to sanitize your request parameter coming from the user, otherwise the application would be vulnerable to directory traversal based attacks
Thanks for tip. I would use them when I am excellent with PHP but for now after reading the Other comments i would stick with switch statements.Thanks by the way
@SamuelHughesMensah You're welcome! It's not that difficult, however :) You just create a directory, putting filter code in there with files named the same as your filters, e.g. "Action.php" and then you include the filter file based on the filter name in the request. An oversimplified version would be merely include("filters/$_GET['filter'].php"); (no security and configurability added here)
1

Wrong use of isset, check documentation, return a boolean.

if (isset($_GET['filter']))
{
    switch ($_GET['filter'])
    {
        case 'Action':
            //TODO
            break;
        case 'Comedy':
            // TODO
            break;
        default:
            // TODO
            break;
    }
}

3 Comments

Not really an answer. Provide only link. Try a comment.
Link only answers are not suited to StackOverflow. They are more suited to comments.
if(isset($_GET['filter'])) { switch($_GET['filter']) { case 'Action': //TODO break; case 'Comedy': // TODO break; } }
0
//isset will always return true or false
if(isset($_GET['filter'])){

    if($_GET['filter']=='Action')
    {
        echo 'Action';

    }elseif($_GET['filter']=='Comedy'){

        echo 'Comedy';

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.