我如何在php中将多个参数传递给URL

时间:2013-12-23 20:16:17

标签: php url parameters

我在通过URL传递2个参数时遇到问题 我不知道怎么做,所以我写了这个:

<form method="get" >
   <b>Enter the Quantity you want:</b>
   <input type="text" name="quantity">
</form>

<br><br>

<a href='./shopping-cart.php?part_id="<?php echo $_GET['part_id']; ?>"&quantity="<?php echo $_GET['quantity']; ?>"'>
   <img src="add_to_shopping_cart.png">
</a>

$_GET['part_id']此var来自其他网址,我想再次传递它,$_GET['quantity']数量来自表单。

3 个答案:

答案 0 :(得分:1)

您必须使用urlencode函数作为参数,不要使用双引号:

<a href='./shopping-cart.php?part_id=<?php echo urlencode($_GET['part_id']); ?>&quantity=<?php echo urlencode($_GET['quantity']); ?>'>

尽管如此,我建议你避免使用长网址,并使用隐藏字段的POST。

答案 1 :(得分:0)

我可以看到一些错误。

如果用户在没有重新加载页面的情况下输入$_GET['quantity'],则无法使用function buildUrl(a) { var qty = document.getElementById("qty").value; a.href = "./shopping-cart.php?part_id="+<?php echo $_GET['part_id']; ?> +"&quantity="+qty; } (请记住,PHP不是客户端)。因此,我建议使用JavaScript。

使用Javascript:

<b>Enter the Quantity you want:</b>
<input type="text" name="quantity">
<a href='#' onclick="buildUrl(this);"> //onclick attribute triggers the JavaScript
    <img src="add_to_shopping_cart.png">
</a> 

由于您不再需要从当前页面获取PHP变量,因此表单已过时,只需要一个简单的链接。

HTML:

{{1}}

答案 2 :(得分:0)

您收到此通知未定义索引:数量因为您尝试访问未定义的变量。 如果您的网页网址类似于page_name.php?quantity=5,则设置$_GET['quantity],否则设置为$_GET

您应该检查if(isset($_GET['quantity'])) { //html code,the url //echo $_GET['quantity'] } 变量是否存在。 如果是这样,请使用@ user4035 answer打印网址。

{{1}}