html链接获取变量地址栏php

时间:2016-01-09 00:43:20

标签: php html variables

包含变量的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>

进入下一页,由于某些原因,即使我点击了option2并且它显示在地址栏中,当我回显citi变量时,我总是得到选项1。我在这里错过了什么吗?

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

1 个答案:

答案 0 :(得分: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;
}