html链接获取可变地址栏php


html link get variable address bar php

拥有包含变量的html链接。

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

它会转到下一页,出于某种原因,当我回显citi变量时,我总是得到选项1,即使我单击了选项2,它也会显示在地址栏中。我错过什么了吗?

$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';}

正如@Barmar所说,这应该是比较而不是赋值。像这个

$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';}

但我建议你使用交换机,因为它工作得更快:

$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;
}