如何在 PHP 的 href 链接中发送多个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11708112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send multiple values in a href link in PHP?
提问by Sahil Chhabra
I want to send multiple values to a different page using a hreflink. But what I retrieve is the first 2 values only, rest shows an error of undefined index.
我想使用href链接将多个值发送到不同的页面。但是我检索的只是前 2 个值,其余显示未定义索引的错误。
My code is:
我的代码是:
<?php
echo "<a href='index.php?choice=search&cat=".$cat."&subcat=".$subcat."&srch=".$srch."&page=".$next."'> Next </a>";
?>
I get the values of "choice" and "cat" only. Please tell me whats wrong in the above code.
我只得到“选择”和“猫”的值。请告诉我上面的代码有什么问题。
回答by Stu
try using urlencode, as if there are any special characters in the strings, it could have an effect on the querystring as a whole;
尝试使用 urlencode,就好像字符串中有任何特殊字符一样,它可能会对整个查询字符串产生影响;
echo "<a href='index.php?choice=search&cat=".urlencode($cat)."&subcat=".urlencode($subcat)."&srch=".urlencode($srch)."&page=".urlencode($next)."'> Next </a>";
Plus you had a space between srch and page, that could have been causing a problem.
另外,您在 srch 和 page 之间有一个空格,这可能会导致问题。
回答by knittl
You must HTML-escape those ampersands properly:
您必须正确地对这些&符号进行 HTML 转义:
?coice=search&cat=...&subcat=...&srch=...
&sub(of &subcat) gets interpreted as ⊂which is a special HTML entity for the subset operator:
&sub(of &subcat) 被解释为⊂which 是子集运算符的特殊 HTML 实体:
⊂ or ⊂ = subset of ?
Also make sure you properly escape your variables to prevent XSS.
还要确保正确转义变量以防止 XSS。

