0

Have html links including a variable.

<a href="cwexchange.php?o=opt1" id="opt1" title="Opt1">Option1</a>
<a href="cwexchange.php?o=opt2" id="opt2" title="Opt2">Option2</a>

which goes to next page, and for some reason I am getting always option 1 when I echo the citi variable even when I have clicked option2 and it shows in the address bar. Did I miss something here?

$cit = "";
$citi = "";
// Make sure the _GET cit is set, and sanitize it
if(isset($_GET["o"])){
$cit = preg_replace('#[^a-z0-9]#i', '', $_GET['o']);
} else {
header("location: example.org");
exit(); 
}

//////////option page routing
if ($cit = "opt1"){$citi = 'Option 1';} 
else if ($cit = "opt2"){$citi = 'Option 2';}
stkmedia
  • 159
  • 2
  • 11

1 Answers1

2

As @Barmar said it should be comparison not assignment. Like this

$cit = "";
$citi = "";
// Make sure the _GET cit is set, and sanitize it
if(isset($_GET["o"])){
$cit = preg_replace('#[^a-z0-9]#i', '', $_GET['o']);
} else {
header("location: example.org");
exit(); 
}

//////////option page routing
if ($cit == "opt1"){$citi = 'Option 1';} 
else if ($cit == "opt2"){$citi = 'Option 2';}

But I suggest you to use switch since it works faster:

$cit = "";
$citi = "";
// Make sure the _GET cit is set, and sanitize it
if(isset($_GET["o"])){
$cit = preg_replace('#[^a-z0-9]#i', '', $_GET['o']);
} else {
header("location: example.org");
exit(); 
}

//////////option page routing
switch ($cit) {
    case 'opt1':
        $citi = 'Option 1';
        break;
    case 'opt2':
        $citi = 'Option 2';
        break;
}
Firat Akandere
  • 523
  • 2
  • 12